0

我编写了一个 xml 文件“test_build.xml”来运行所有测试。我已经编译了测试文件和编译的源文件。我还需要一些我的源/测试文件使用的 jar 文件。我已在路径中包含所有这些目录(编译代码)/jar 并将其用作我的类路径。我得到一个运行时异常。

<项目名称="myandroid" >

<path id="test.classpath">
    <pathelement path="a.jar"/>
    <pathelement path="tests"/> 
    <!-- other jars and directories -->
</path>
<target name="test-run" description="Run Test">
    <delete dir = "test_result" />
    <mkdir dir = "test_result" />
    <junit printsummary="yes" haltonfailure="yes" showoutput="yes" >
        <classpath refid="test.classpath"/>
        <batchtest fork="yes" todir="test_result">
            <formatter type="xml"/>
            <fileset dir="tests">
                <include name="**/*.java"/>
            </fileset>
        </batchtest>
    </junit>
</target>

我在命令行上运行以下命令: ant "test-run" -f test_build.xml

得到:

测试运行:

[delete] Deleting directory test_result
[mkdir] Created dir: test_result
[junit] Exception in thread "main" java.lang.RuntimeException: Stub!
[junit]     at junit.framework.TestResult.<init>(TestResult.java:4)
[junit]     at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.run(JUnitTestRunner.java:353)
[junit]     at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.launch(JUnitTestRunner.java:1052)
[junit]     at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(JUnitTestRunner.java:906)
[junit] Running RegexTest
[junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
4

2 回答 2

1

The android.jar file that you use to create the application does only contain stub implementations.

public void something(...) { 
    throw new RuntimeException("Stub!"); 
} 

Stubbing the implementations reduces the size of the SDK a lot. This is fine for building an app as you still build against Android's public API. To finally execute your APK or run tests again, it must be deployed on either a device or emulator.

Update

To actually solve the problem you need an attached device or running emulator instance on your laptop. Verify that it's accessible by listing all available devices with adb:

$ adb devices

The run-tests target should automatically deploy your application on an emulator or device. If it does not you probably have to update your project first. The Android documentation, section Running Tests, states:

You can use Ant to run all the tests in your test project, using the target run-tests, which is created automatically when you create a test project with the android tool.

This target re-builds your main project and test project if necessary, installs the test application to the current AVD or device, and then runs all the test classes in the test application.

You can update an existing test project to use this feature. To do this, use the android tool with the update test-project option.

于 2013-02-01T08:32:32.463 回答
0

通过首先放置测试类路径来反转您的路径声明!
应该正常工作;-)

于 2013-11-15T12:20:11.553 回答