当前位置: 动力学知识库 > 问答 > 编程问答 >

Android unit testing - how to run tests in same project as application?

问题描述:

In the Android LunarLander sample project, the unit tests are included right in the project, in a (non-source) folder called 'tests'. This is in line with the SDK testing guide which recommends this layout as opposed to creating tests in a separate project. However, I have no idea how I can actually run these tests.

I can't create an Android Junit Test run configuration:

  • if I try to 'run all test in project or package' it complains that manifest file doesn't contain instrumentation info - clearly it's using the top-level manifest file instead of the tests manifest file.
  • if I try to 'run a single test', I can't find any because tests isn't a source folder, and if I set it as a source folder, errors pop up, since it assumes the test class should be in a package starting with 'tests.src'.

It's starting to seem to me that this sample is broken... I hope I am wrong, since I'd rather embed tests into my project and be able to run them easily (instead of creating a separate test project that links to project for application under test). Does anybody know how I can run these tests? Thanks...

网友答案:

Google recommend a single all-in-one directory because it makes your files easy maintainable in many situation, for instance when dealing with source control.

It doesn't matter where the test project is located int the file system, however, you must import it into your Eclipse's workspace, same as what you did for the LunarLander project:

If everything goes well, your Package Explorer should look something like this:

In my Android 4.2 samples, things are not going well, it seems that the source code of LunarLander test project is not up-to-date:

  1. it doesn't come with project.properties file.
  2. it uses same package name as LunarLander project, resulting Eclipse to be fooled when importing package/class from the referenced LunarLander project.
  3. it doesn't automatically add the LunarLander project to test project's classpath, resulting imported package/class from the referenced LunarLander project is invisible.

Once you resolve all issues, you should able to run/debug Android JUnit Test from test project.

分享给朋友:
您可能感兴趣的文章:
随机阅读: