如何创建Android JUnit测试用例来测试Activity中生成的Intent的内容?
我有一个包含EditText窗口的Activity,当用户输入完所需的数据后,Activity会向IntentService启动一个Intent,它会记录数据并继续执行应用程序.这是我想要测试的类,OnEditorActionListener/PasscodeEditorListener是作为一个单独的类创建的:
public class PasscodeActivity extends BaseActivity {
EditText m_textEntry = null;
PasscodeEditorListener m_passcodeEditorListener = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.passcode_activity);
m_passcodeEditorListener = new PasscodeEditorListener();
m_textEntry = (EditText) findViewById(R.id.passcode_activity_edit_text);
m_textEntry.setTag(this);
m_textEntry.setOnEditorActionListener(m_passcodeEditorListener);
}
@Override
protected void onPause() {
super.onPause();
/*
* If we're covered for any reason during the passcode entry,
* exit the activity AND the application...
*/
Intent finishApp = new Intent(this, CoreService.class);
finishApp.setAction(AppConstants.INTENT_ACTION_ACTIVITY_REQUESTS_SERVICE_STOP);
startService(finishApp);
finish();
}
}
class PasscodeEditorListener implements OnEditorActionListener{
@Override
public boolean …Run Code Online (Sandbox Code Playgroud) 我想测试一个CommentActivity通常构造和使用实例的Android活动CommentsDataSource(两者都是我写的类).
public class CommentActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
:
CommentsDataSource = new CommentsDataSource(..);
:
}
:
}
Run Code Online (Sandbox Code Playgroud)
我愿意创建MockCommentsDataSource自己,并希望避免使用第三方模拟框架.(为什么?因为我的教学试图减少我需要填写的学期信息量以及学生需要安装的软件数量.我看过其他帖子推荐Guice,roboguice和Spring.)
我的问题是如何将CommentsDataSource(或MockCommentsDataSource)传递给Activity.制作它们似乎不切实际,Serializable或者Parcelable它们必须是为了通过Intent它开始传递它们CommentActivity.虽然我可以很容易地传入一个调试标志,但使用它需要CommentActivity知道MockCommentsDataSource,这实际上不是它的业务(并且在一个单独的应用程序中):
public class CommentActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
:
debugMode = getIntent().getBooleanExtra(DEBUG_MODE, false);
// Get a connection to the database.
final CommentsDataSource cds = (debugMode ?
new MockCommentsDataSource() …Run Code Online (Sandbox Code Playgroud) android dependency-injection abstract-factory android-testing
Google开发者支持非常明确:
Alpha/Beta可用性
Alpha和beta APK需要具有比生产APK更高的版本代码才能进行测试.
要为alpha测试人员提供服务,alpha APK需要拥有比beta APK更高的版本代码.
如果使用比alpha APK更高的版本代码上传测试版APK,则会自动停用Alpha版本.
- 如果使用比alpha或beta APK更高的版本代码上传制作APK,则会自动停用Alpha和/或Beta版本.
Alpha测试组用户有资格使用与其设备兼容的最高版本代码的应用的alpha版,测试版或生产版.Beta测试组用户只能使用与其设备兼容的最高版本代码的应用的测试版或生产版.
我的问题是,有没有办法区分具有相同二进制或APK的自定义参数的Alpha/Beta版本和生产版本?当我的意思是自定义参数时,它会像指向测试或生产云端点的URL.
问题是,否则我必须根据配置文件发布不同的APK,并且当我发布到生产alpha/beta测试人员时,将获得指向生产云端点的更新,其中数据在测试中.我将不得不发布一个生产版本,然后是另一个alpha/beta版本来解决这个问题,但我发现它非常无聊.
我正在尝试使用浓缩咖啡和junit4.问题似乎是我在activity.onCreate中静态初始化了代码并在activity.onDestroy中取消了初始化
现在,当我运行espresso时,测试似乎是"并行"运行.我将日志类添加到活动onCreate和onDestroy.
我所看到的是
onCreate = example.package.MainActivity@ABC
onCreate = example.package.MainActivity@JKL
onDestroy = example.package.MainActivity@ABC
onCreate = example.package.MainActivity@XYZ
onDestroy = example.package.MainActivity@JKL
onDestroy = example.package.MainActivity@XYZ
Run Code Online (Sandbox Code Playgroud)
当然,在第三次测试中它在NPE上失败,因为第二次测试的onDestroy在第三次onCreate之后运行(这使得静态代码无效)
这种行为是否正常?我可以先将浓缩咖啡强制用于拆解活动实例,然后再开始新的吗?谢谢!
如何在Android自动化测试中从Appium中的软键盘发送/按Enter键?
我尝试了几个选项,但它们都没有工作 - 而不是按键,它们正在清除在文本区域中输入的文本.下面是代码(使用JAVA语言):
String app_package_name = "abc.xyz.android";
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("deviceName", "Nexus_5X_API_23");
capabilities.setCapability("platformVersion", "6.0");
capabilities.setCapability("appPackage", app_package_name);
capabilities.setCapability("appActivity", app_package_name + ".activity.StartupActivity_");
String url = "http://127.0.0.1:4723/wd/hub";
AndroidDriver driver = new AndroidDriver(new URL(url), capabilities);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
By password = By.id(app_package_name_with_id + "et_password");
WebElement enterPassword = driver.findElement(password);
enterPassword.click();
driver.getKeyboard().sendKeys("12345");
driver.getKeyboard().sendKeys(Keys.ENTER); // THIS IS NOT WORKING.
driver.getKeyboard().sendKeys(Keys.RETURN); // THIS IS ALSO NOT WORKING.
driver.pressKeyCode(AndroidKeyCode.ENTER); // THIS IS ALSO NOT WORKING.
driver.pressKeyCode(AndroidKeyCode.KEYCODE_NUMPAD_ENTER); // SAME HERE.
enterPassword.sendKeys(Keys.ENTER); // SAME HERE. …Run Code Online (Sandbox Code Playgroud) 我正在开发一个项目,我们正在尝试使用ANDROID TEST ORCHESTRATOR,因为它可以隔离崩溃的明显好处.但是在执行测试套件时,在我看来,当协调器为每个测试用例启动一个新进程时,套件执行的测试覆盖率报告总是显示不完整的数据(主要是测试中出现的最后一个测试用例的数据)套房).
所以我想知道有没有办法克服这个问题,并为测试套件中现有的所有仪器化测试生成一个jacoco代码覆盖率报告.
android code-coverage orchestration android-testing android-instrumentation
我有这个模拟课:
class MockCategoriesRepository implements CategoriesRepository {
@Override
public LiveData<List<Category>> getAllCategories() {
List<Category> categories = new ArrayList<>();
categories.add(new Category());
categories.add(new Category());
categories.add(new Category());
MutableLiveData<List<Category>> liveData = new MutableLiveData<>();
liveData.setValue(categories);
return liveData;
}
}
Run Code Online (Sandbox Code Playgroud)
和测试:
@Test
public void getAllCategories() {
CategoriesRepository categoriesRepository = new MockCategoriesRepository();
LiveData<List<Category>> allCategories = categoriesRepository.getAllCategories();
}
Run Code Online (Sandbox Code Playgroud)
我想测试List<Category>空.
我该怎么做?我可以使用Mockito吗?
我突然想通了这个宣言:
<uses-library
android:name="com.android.nfc_extras"
android:required="false"/>
Run Code Online (Sandbox Code Playgroud)
<application>我的内部范围AndroidManifest.xml使仪器测试失败:
java.lang.NoSuchMethodError: No static method allOf(Lorg/hamcrest/Matcher;Lorg/hamcrest/Matcher;)Lorg/hamcrest/Matcher; in class Lorg/hamcrest/core/AllOf; or its super classes (declaration of 'org.hamcrest.core.AllOf' appears in /system/framework/com.android.nfc_extras.jar)
at org.hamcrest.Matchers.allOf(Matchers.java:33)
at android.support.test.espresso.Espresso.<clinit>(Espresso.java:187)
at android.support.test.espresso.Espresso.onView(Espresso.java:75)
at com.example.abusik.espressotest.InstrumentedTest.changeText_sameActivity(InstrumentedTest.kt:34)
at java.lang.reflect.Method.invoke(Native Method)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
Run Code Online (Sandbox Code Playgroud)
我现在想出的是:
我的InstrumentedTest.kt档案:
package com.example.abusik.espressotest
import android.support.test.espresso.Espresso.onView
import android.support.test.espresso.action.ViewActions.*
import android.support.test.espresso.assertion.ViewAssertions.matches
import android.support.test.espresso.matcher.ViewMatchers.withId
import …Run Code Online (Sandbox Code Playgroud) 我一直在想如何正确地测试依赖于Android的日期或时间变化的功能.假设我有每个月的第一天需要处理的事件,这个处理的结果取决于当前的日期/时间.我错过了在Android测试框架中伪造某个日期/时间的可能性.我们该如何测试那种东西?
当然,可以通过不是直接从框架查询日期/时间而是从另外的实体查询日期/时间来实时抽象.可以在测试代码中为此实体设置假日期/时间.但是,Android测试框架中是否真的不支持这样的常见需求?
我需要部署测试应用程序并在连接到同一网络中另一台计算机的设备上发出命令.
我通过http://developer.android.com/tools/help/adb.html#directingcommands阅读,但我无法找到答案.
我尝试过使用adb connect <remote machine IP>但是我收到了unable to connect错误.
有没有办法adb在连接到远程系统的设备上部署应用程序和执行命令?
android ×10
android-testing ×10
adb ×1
android-date ×1
appium ×1
automation ×1
google-play ×1
junit ×1
junit4 ×1
kotlin ×1
nfc ×1
selenium ×1