[英]Robolectric tests running in Android Studio but not on the command line
I'm trying to run unit tests using Robolectric; 我正在尝试使用Robolectric进行单元测试; they run fine under Android Studio, but the exact same tests fail when running in the command line - which is a big deal, I need to be able to run them from my continuous integration platform, not just from an IDE. 它们在Android Studio下运行正常,但在命令行中运行时完全相同的测试失败 - 这是一个大问题,我需要能够从我的持续集成平台运行它们,而不仅仅是从IDE运行它们。
I suspect that I'm missing some command-line argument (say, a classpath or something similar) or calling the wrong task - otherwise the test wouldn't run at all from Android Studio. 我怀疑我缺少一些命令行参数(例如,类路径或类似的东西)或调用错误的任务 - 否则测试将无法从Android Studio运行。 Some relevant details; 一些相关细节; the test looks like this: 测试看起来像这样:
@RunWith(RobolectricTestRunner.class)
@Config(manifest = "app/src/main/AndroidManifest.xml", resourceDir = "res", emulateSdk = 19)
public class SplashActivityTest {
@Test
public void testActivity() {
SplashActivity splashActivity = new SplashActivity();
String appName = splashActivity.getString(R.string.app_name); // HERE, line 20
assertEquals(appName, "App");
}
}
As mentioned above, it runs fine in Android Studio (by right-clicking the test file and selecting Run 'SplashActivityTest'
) but when running it from the command line it fails in the line marked with HERE
, with the following stack trace: 如上所述,它在Android Studio中正常运行(通过右键单击测试文件并选择Run 'SplashActivityTest'
),但是当从命令行运行它时,它会在标有HERE
的行中失败,并显示以下堆栈跟踪:
android.content.res.Resources$NotFoundException: unknown resource 2131492893
at org.robolectric.shadows.ShadowAssetManager.getAndResolve(ShadowAssetManager.java:309)
at org.robolectric.shadows.ShadowAssetManager.getResourceText(ShadowAssetManager.java:69)
at android.content.res.AssetManager.getResourceText(AssetManager.java)
at android.content.res.Resources.getText(Resources.java:240)
at org.robolectric.shadows.ShadowResources.getText(ShadowResources.java:361)
at android.content.res.Resources.getText(Resources.java)
at android.content.res.Resources.getString(Resources.java:330)
at org.robolectric.shadows.ShadowContext.getString(ShadowContext.java:39)
at org.robolectric.shadows.ShadowContextWrapper.getString(ShadowContextWrapper.java:69)
at android.content.Context.getString(Context.java)
at path.to.myApp.activities.SplashActivityTest.testActivity(SplashActivityTest.java:20)
// ... and so on ...
I'm using this to run from the command line (notice that in here and in Android Studio I'm using the Gradle wrapper): 我正在使用它从命令行运行(请注意,在这里和Android Studio中我使用的是Gradle包装器):
project-root$ ./gradlew test --continue
Also: I'm using Android Studio 1.1.0
, Gradle version is 2.3
, Robolectric's version is 3.0-SNAPSHOT
and Robolectric's Gradle plugin version is 1.0.1
另外:我使用的是Android Studio 1.1.0
,Gradle版本是2.3
,Robolectric的版本是3.0-SNAPSHOT
而Robolectric的Gradle插件版本是1.0.1
It turns out that this is a known issue. 事实证明,这是一个已知问题。 The resources of a project are looked for in the wrong directory when running tests from the command line, here's a link to the issue; 从命令行运行测试时,在错误的目录中查找项目的资源,这是指向该问题的链接 ; for now the workaround is to write a custom test runner as demonstrated here : 对于现在的解决方法是编写自定义的测试运行的证明这里 :
public class RobolectricGradleTestRunner extends RobolectricTestRunner {
public RobolectricGradleTestRunner(Class<?> testClass) throws InitializationError {
super(testClass);
String buildVariant = (BuildConfig.FLAVOR.isEmpty() ? "" : BuildConfig.FLAVOR+ "/") + BuildConfig.BUILD_TYPE;
String intermediatesPath = BuildConfig.class.getResource("").toString().replace("file:", "");
intermediatesPath = intermediatesPath.substring(0, intermediatesPath.indexOf("/classes"));
System.setProperty("android.package", BuildConfig.APPLICATION_ID);
System.setProperty("android.manifest", intermediatesPath + "/manifests/full/" + buildVariant + "/AndroidManifest.xml");
System.setProperty("android.resources", intermediatesPath + "/res/" + buildVariant);
System.setProperty("android.assets", intermediatesPath + "/assets/" + buildVariant);
}
}
The test cases must be updated accordingly to use the custom test runner instead of the @Config
annotation: 必须相应地更新测试用例以使用自定义测试运行器而不是 @Config
注释:
@RunWith(RobolectricGradleTestRunner.class)
public class SplashActivityTest {
// tests same as before
}
Also, it's recommended to perform a clean
when running the tests from the command line, given that the workaround depends on the contents of the build directory, we don't want to have stale data there: 此外,建议在从命令行运行测试时执行clean
,假设解决方法取决于构建目录的内容,我们不希望在那里有过时的数据:
project-root$ ./gradlew clean test --continue
I've solved the same error, but in different way. 我已经解决了同样的错误,但方式不同。 I used custom class shadows in tests, So I have to create my own TestRunner class, like this: 我在测试中使用了自定义类阴影,因此我必须创建自己的TestRunner类,如下所示:
public class MyRobolectricTestRunner extends RobolectricTestRunner {
public MyRobolectricTestRunner(Class<?> klass) throws InitializationError {
super(klass);
}
public InstrumentationConfiguration createClassLoaderConfig() {
InstrumentationConfiguration.Builder builder = InstrumentationConfiguration.newBuilder();
builder.addInstrumentedClass(AppUtils.class.getName());
return builder.build();
}
}
But above code worked only when I run the unit-test from Andorid studio. 但是上面的代码只有在我从Andorid工作室运行单元测试时才有效。 Please, check out code that works for me in both ways: in Studio and from the command-line : 请查看适用于我的代码:在Studio中和从命令行 :
public class MyRobolectricTestRunner extends RobolectricTestRunner {
public MyRobolectricTestRunner(Class<?> klass) throws InitializationError {
super(klass);
}
@Override
public InstrumentationConfiguration createClassLoaderConfig() {
InstrumentationConfiguration.Builder builder = InstrumentationConfiguration.newBuilder();
builder.addInstrumentedPackage(com.example.stackoverflow.AppUtils.class.getPackage().getName());
return builder.build();
}
}
Please, note I have the dependency: 请注意我有依赖:
testCompile 'org.robolectric:robolectric:3.0'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.