Mar*_*sch 207 junit android android-testing androidx
对于我正在使用的仪器测试
@RunWith(AndroidJUnit4.class)
Run Code Online (Sandbox Code Playgroud)
从
import androidx.test.runner.AndroidJUnit4;
Run Code Online (Sandbox Code Playgroud)
为了建立我的测试用例.现在,这条线被标记为与提示使用过时AndroidJUnit4的
import androidx.test.ext.junit.runners.AndroidJUnit4
Run Code Online (Sandbox Code Playgroud)
但是,如果我尝试AndroidJUnit4从命名包导入我得到错误,ext无法解决.
您是否有任何想法,应该在gradle中包含哪些包来解决此问题?
Mar*_*sch 296
我在gradle文件中添加了以下行:
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
Run Code Online (Sandbox Code Playgroud)
现在一切都恢复了.
JJ *_*sis 22
如果您尝试过@MarcelGangwisch的解决方案,并且构建失败说它找不到资源,并且您还清理/重建了项目,但仍然无法正常工作,请尝试以下方法:(也基于@KrzysztofDziuba的解决方案)
在更改依赖关系的gradle文件中,确保将其添加为所需的类型,即:
对于UI测试:
androidTestImplementation'androidx.test.ext:junit:1.1.0'
对于单元测试:
testImplementation'androidx.test.ext:junit:1.1.0'
在我的实例中,我将两者同时添加,现在可以使用了。
小智 20
解决方案
将此行添加到 build.gradle 中: androidTestImplementation 'androidx.test.ext:junit:1.1.1'
在测试类中更改AndroidJUnit4为AndroidJUnit4ClassRunner
警告
我遇到了同样的错误,但以下解决方案对我来说失败了:
File -> Invalidate Caches...,然后选择 Invalidate and Restart
对我而言,以下步骤有效:
1.将androidx库替换为此处发布的库。我的决赛app/build.gradle看起来像这样:
android {
...
defaultConfig {
...
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
...
}
dependencies {
...
testImplementation 'junit:junit:4.12'
// Core library
androidTestImplementation 'androidx.test:core:1.2.0'
// AndroidJUnitRunner and JUnit Rules
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test:rules:1.2.0'
// Assertions
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.ext:truth:1.2.0'
androidTestImplementation 'com.google.truth:truth:0.42'
// Espresso dependencies
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
Run Code Online (Sandbox Code Playgroud)
然后,我将ExampleInstrumentTest.java班级中导入的模块手动替换为最新班级:
import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.rule.ActivityTestRule;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(AndroidJUnit4ClassRunner.class)
public class ExampleInstrumentedTest {
...
@Rule
public final ActivityTestRule<MainActivity> main = new ActivityTestRule<>(MainActivity.class, true);
@Before
public void init() {
...
}
@Test
public void listCount() {
...
}
@Test
public void useAppContext() {
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
Assert.assertEquals("in.curioustools.aad_x_testing2", appContext.getPackageName());
System.out.println("useAppContext : Test Ran");
}
}
Run Code Online (Sandbox Code Playgroud)
让我烦恼的是,InstrumentationRegistery该类仍被弃用。所以我InstrumentationRegistry.getInstrumentation().getTargetContext();从androidx.test.platform.app.InstrumentationRegistry课堂上使用过。
小智 7
将这些行包含在 build.gradle 应用程序模块中。
testImplementation 'androidx.test.ext:junit:1.1.1' androidTestImplementation 'androidx.test.ext:junit:1.1.1'
在测试类中将您的测试运行程序替换为
导入 androidx.test.ext.junit.runners.AndroidJUnit4;
如果InstrumentationRegistry.getTargetContext()不推荐使用InstrumentationRegistry这样androidx.test.platform.app的
InstrumentationRegistry.getInstrumentation().getTargetContext()
就我而言,改变androidTestImplementation为testImplementation有帮助。在阅读build.gradle 中 testImplementation 和 androidTestImplementation 之间的 android 差异之前,我不知道有什么区别
| 归档时间: |
|
| 查看次数: |
37425 次 |
| 最近记录: |