如何使用Robolectric 和 PIT测试 Android 应用程序?
使用 Robolectric,您可以在 JVM 中运行 Android 测试。使用 PIT,您可以显示线路覆盖率并进行突变测试。对我来说,可以使用 Eclipse+Plugins,但没有要求。
这是我到目前为止所尝试的:
我有一个 Android 项目,我们称之为MyProject。
我现在想使用Robolectric 和 PIT在 JVM 中测试 MyProject 。因此,我创建了另一个名为MyTest的项目,并成功地运行了 Robolectric 测试,正如robolectric 快速入门中所述。这my.app.tests.MyActivityTest
看起来像:
@RunWith(RobolectricTestRunner.class)
public class MyActivityTest {
@Test
public void myTest() throws Exception {
String appName = new MainActivity().getResources().getString(R.string.app_name);
Assert.assertEquals(appName, "MyProject");
}
}
现在是棘手的部分:我想将 PIT 的线路覆盖和突变测试添加到我的 Robolectric 测试中。首先尝试使用Pitclipse - 没有运气。Pitclipse 似乎还不支持 Eclipse Project Depencies。
所以我的第二次尝试是使用命令行,如PIT quick start中所述:
首先,我确保我的测试通过命令行使用 Junit 成功运行:
java -cp <classpath> org.junit.runner.JUnitCore my.app.tests.MyActivityTest
其中<classpath>
包含:junit4、robolectric、MyProject 类文件、MyTest 类文件、android.jar 和其他必要的android 库。
一旦这个 JUnit 测试成功,我<classpath>
在 PIT 调用中使用了相同的方法,并在 MyProject 的根路径中执行该调用:
java -cp ../MyTest/bin:../MyTest/libs/*:bin/classes:~/android-sdk-linux/platforms/android-17/android.jar \
org.pitest.mutationtest.MutationCoverageReport \
--reportDir ../MyTest/pit-report \
--targetClasses my.app.* \ # package in MyProject
--targetTests my.app.tests.* \ # package in MyTest
--sourceDirs src/
但是,这会导致我在下面发布的异常。我想我需要使用 PIT 的--excludedClasses
参数排除一些类,但没有提示哪个类可能会导致问题。请注意,MyActivityTest
它没有超类,也没有显式构造函数。
java.lang.NullPointerException
ERROR Description [testClass=my.app.tests.MyActivityTest, name=myTest(my.app.tests.MyActivityTest)] -> java.lang.NullPointerException
at org.pitest.boot.CodeCoverageStore.visitProbes(CodeCoverageStore.java:92)
at my.app.tests.MyActivityTest.<init>(MyActivityTest.java:22)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:532)
at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:195)
at org.robolectric.RobolectricTestRunner$HelperTestRunner.createTest(RobolectricTestRunner.java:647)
at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:244)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:241)
at org.robolectric.RobolectricTestRunner$HelperTestRunner.methodBlock(RobolectricTestRunner.java:657)
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:227)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:175)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.pitest.junit.adapter.CustomRunnerExecutor.run(CustomRunnerExecutor.java:42)
at org.pitest.junit.adapter.AdaptedJUnitTestUnit.execute(AdaptedJUnitTestUnit.java:86)
at org.pitest.coverage.execute.CoverageDecorator.execute(CoverageDecorator.java:50)
at org.pitest.containers.UnContainer.submit(UnContainer.java:46)
at org.pitest.Pitest$3.run(Pitest.java:148)
at java.lang.Thread.run(Thread.java:679)