在我的 Android 应用程序中,我有几个测试类。如果我运行以下命令,./gradlew connectedAndroidTest它将运行 android test 文件夹中的所有测试用例并为所有测试类生成测试报告,但我需要测试特定的测试类并为我尝试使用以下命令的测试类生成报告:
./gradlew test --tests com.login.user.UserLoginTest
Run Code Online (Sandbox Code Playgroud)
上面的命令抛出以下异常
FAILURE: Build failed with an exception.
* What went wrong:
Problem configuring task :app:test from command line.
> Unknown command-line option '--tests'.
* Try:
Run gradlew help --task :app:test to get task usage details. Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at …Run Code Online (Sandbox Code Playgroud) android unit-testing android-gradle-plugin android-instrumentation android-junit
我有一个InstrumentedTest
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void useAppContext(String groupName) {
Context appContext = InstrumentationRegistry.getTargetContext();
UiDevice device=UiDevice.getInstance(getInstrumentation());
...
...
}
}
Run Code Online (Sandbox Code Playgroud)
我想用adb shell命令执行它.但我要传递groupName方法的参数值useAppContext(String groupName)
我正在使用命令
adb shell am instrument -w -r -e debug false -e class 'com.<package_name>.ExampleInstrumentedTest' com.<package_name>.test/android.support.test.runner.AndroidJUnitRunner
Run Code Online (Sandbox Code Playgroud)
但是如何将方法参数作为参数传递给在命令提示符下运行的命令?
我尝试在Android Studio 1.5.1中使用espresso 2.2.1运行测试.当我运行LoginActivityTest时,我收到此错误:当LoginActivity调用MyService.java并且MyService需要整数资源(即R.integer.number_of_days)时,会导致"android.content.res.Resources $ NotFoundException".此资源在gradle(版本1.5.0)模块的R.integer.xml文件中定义.
项目结构:
RootFolder/
?----projectA/
? ?----build.gradle
? ?----settings.gradle
? ?----src/androidTest/java/.../LoginActivityTest
? ?----src/main/java/.../LoginActivity
?
?----Module/
?----krill/
? ?----build.gradle
? ?----settings.gradle
? ?----src/main/
| ?----java/service/MyService.java
| ?----res/value/integers.xml
?
?----otherModule/
?----build.gradle
我的考试班:
@RunWith(AndroidJUnit4.class)
@LargeTest
public class LoginActivityTest extends ActivityInstrumentationTestCase2<LoginActivity >{
@Rule
public ActivityTestRule<LoginActivity> mActivityRule = new ActivityTestRule(LoginActivity.class);
public LoginActivityTest() {
super(LoginActivity.class);
}
@Test
public void testConfigDialog() {
onView(withId(R.layout.login_custom_view));
onView(withId(R.id.id_username)).perform(clearText());
onView(withId(R.id.id_password)).perform(clearText());
}
}
Run Code Online (Sandbox Code Playgroud)
错误堆栈跟踪:
Running tests
Test running started
android.content.res.Resources$NotFoundException: Resource ID #0x7f090004
at android.content.res.Resources.getValue(Resources.java:1233)
at android.content.res.Resources.getInteger(Resources.java:989)
at it.company.android.lib.auth.infrastructure.AuthenticatorPreferences.<init>(MyService.java:36) …Run Code Online (Sandbox Code Playgroud) android gradle android-studio android-espresso android-instrumentation
对于检测测试,我有一个TestApplication创建一个TestComponent,但不再生成文件(Error:/xxx/TestApplication.java:16: The import.xxx.DaggerTestApplicationComponent cannot be resolved).我无法确定根本原因.我尝试了不同的Android Studio(2.2,2.1.2),不同的gradle插件(2.2.0-alpha6,5,4)和不同版本的dagger(2.2到2.6).
我应该用androidTestAnnotationProcessor吗?(以前不是这种情况)
编辑:使用dagger 2.6,需要添加 classpath 'com.google.guava:guava:19.0'
更新:a出现问题Module,因此Component无法创建.但是,使用插孔(即使有调试选项),我也看不到问题.现在,恢复到java 7,gradle插件2.1.2.这样,无需指定哪个番石榴版本,以及所有最新的库都可以使用(dagger 2.6,butterknife 8.2.1,apt 1.8)
android android-gradle-plugin android-instrumentation dagger-2 annotation-processor
我正在尝试为我的应用添加loginfacebook.但是当我添加一个需要这样做的存储库时.它导致了一个错误.AndroidJUnit4现在无法解决.
ExampleInstrumentedTest.java
package com.example.user.enyatravelbataan;
import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;
import static junit.framework.Assert.assertEquals;
import static org.junit.Assert.*;
/**
* Instrumentation test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void useAppContext() throws Exception {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getTargetContext();
assertEquals("com.example.user.enyatravelbataan",
appContext.getPackageName());
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的构建:gradle(app)
apply plugin: 'com.android.application'
android {
compileSdkVersion 24
buildToolsVersion "24.0.3"
useLibrary 'org.apache.http.legacy'
repositories {
mavenCentral() …Run Code Online (Sandbox Code Playgroud) java junit4 facebook-login android-studio android-instrumentation
我的目标是使用工具测试(AndroidJUnitRunner)为Android CPU密集型代码编写自动性能测试.
我很惊讶地发现测试结果不可靠,模拟CPU密集型代码,我想测试一下,我写了下面的循环
for(int i=0;i<1000000;i++){
Math.pow(2,i);
}
Run Code Online (Sandbox Code Playgroud)
该代码在Android应用程序中作为工具测试进行了测试
我得到的结果如下:
仪器测试显示〜230ms完成循环,而同一设备(G5)上的相同代码耗时约600ms
我将非常感谢为什么AndroidJUnitRunner上相同代码的执行时间比真实设备少三倍,而它们最终都在同一台设备上执行
我尝试在Android Studio上使用espresso框架(和Junit4)进行片状测试.
我想设置重复次数.在我可以使用之前
@FlakyTest(公差= 5)
//(例如,5是重复的数字)
但是这个注释在API级别24中已被弃用. - (在android.developers.com上链接)
现在有了新的@FlakyTest注释 - 没有容差变量.(链接在android.developers.com上)
我需要设置多少次测试可以重复,但不知道怎么做.任何的想法?
junit android android-testing android-espresso android-instrumentation
对于 AndroidX,InstrumentationRegistry 现在已弃用。该文件指出
此方法已弃用。在大多数情况下,应该使用 getApplicationContext() 而不是仪器测试上下文。如果您确实需要访问测试上下文以访问其资源,则建议改用 getResourcesForApplication(String)。
但是,我找不到有关如何获取PackageManager要调用的in test实例getResourcesForApplication以及应为其字符串参数提供哪个包名称的任何示例。
例如,这是当前有效的代码:
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.io.IOException;
import java.io.InputStream;
import androidx.test.InstrumentationRegistry;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import static org.junit.Assert.*;
@RunWith(AndroidJUnit4.class)
public class MyTest {
@Test
public void processImage() {
// load image from test assets
AssetManager am = InstrumentationRegistry.getContext().getAssets();
InputStream is = null;
Bitmap image = null;
try {
is = am.open("image.jpg");
image = BitmapFactory.decodeStream(is);
} catch (IOException e) { …Run Code Online (Sandbox Code Playgroud) 我正在开发一个项目,我们正在尝试使用ANDROID TEST ORCHESTRATOR,因为它可以隔离崩溃的明显好处.但是在执行测试套件时,在我看来,当协调器为每个测试用例启动一个新进程时,套件执行的测试覆盖率报告总是显示不完整的数据(主要是测试中出现的最后一个测试用例的数据)套房).
所以我想知道有没有办法克服这个问题,并为测试套件中现有的所有仪器化测试生成一个jacoco代码覆盖率报告.
android code-coverage orchestration android-testing android-instrumentation
我有一个多模块项目。我希望能够分别运行我为这些模块编写的仪器测试。当我运行测试并初始化活动时,我不断遇到以下错误。
默认 FirebaseApp 在此过程中未初始化。确保首先调用 FirebaseApp.initializeApp(Context)。
我尝试在测试代码中使用测试应用程序的上下文显式调用此函数,但无济于事。我知道 firebase 是通过配置文件设置的,但我没有明确调用它来设置它。在实际应用程序启动之前,它会通过内容提供商(阅读博客)自动初始化。
因为这将是一个静态调用,并且我们没有显式调用它,所以我也看不到模拟它的方法。有没有办法在我的模块中完全禁用 UITest 运行的 firebase ?