[英]Robolectric ignores custom and test application class when running more than one test
For an Android app, I have Robolectric unit-tests. 对于Android应用程序,我有Robolectric单元测试。 I run them from Eclipse.
我从Eclipse运行它们。
I'm using the Robolectric feature of providing a replacement Application
class for Robolectric to use. 我正在使用Robolectric功能为Robolectric提供替代
Application
类。 This class, call it TestApplication
, extends my application class, call it MyApplication
, which extends the Android Application
class. 这个类称为
TestApplication
,它扩展了我的应用程序类,称之为MyApplication
,它扩展了Android Application
类。
I've found that, when I run individual test cases, Robolectric uses the substitute Application class as expected. 我发现,当我运行单个测试用例时,Robolectric按预期使用替代Application类。 But, if I create a run configuration for all the tests in the project, Robolectric reverts to using the Android
Application
class. 但是,如果我为项目中的所有测试创建运行配置,Robolectric将恢复使用Android
Application
类。 It ignores both TestApplication
and MyApplication
. 它忽略了
TestApplication
和MyApplication
。
What could be causing this? 可能是什么导致了这个? Thanks in advance!
提前致谢!
I encountered a similar problem with robolectric where the suite of tests would fail but a single test would run fine. 我遇到了与robolectric类似的问题,其中测试套件会失败,但单个测试运行正常。 It turned out there was a timing issues while running the suite.
事实证明,在运行套件时存在时间问题。 Try adding a wait timer or a sleep and see if it works
尝试添加等待计时器或睡眠,看看它是否有效
It seems the cause of the problem was that Robolectric was not using my project's AndroidManifest.xml when Eclipse ran a "run configuration" for a package or an entire project, as opposed to a single test class. 问题的原因似乎是当Eclipse为包或整个项目运行“运行配置”而不是单个测试类时,Robolectric没有使用我的项目的AndroidManifest.xml。
A fix that works is to use Robolectric's configuration feature to provide the location of the manifest. 可行的修复方法是使用Robolectric的配置功能来提供清单的位置。 See here .
看到这里 。
Both the @Config
and properties file solutions worked for me. @Config
和属性文件解决方案都适合我。 I preferred the properties file because it applies to the entire project, whereas the annotation has to be on every test class. 我更喜欢属性文件,因为它适用于整个项目,而注释必须在每个测试类上。
The properties file should be named "org.robolectric.Config.properties" and you should put it on your classpath (I did this by placing the file in the test project's source folder). 属性文件应该命名为“org.robolectric.Config.properties”,你应该把它放在你的类路径上(我把它放在测试项目的源文件夹中)。
Assuming you have a TestMyProject
containing Robolectric tests for MyProject
, the file should contain the following. 假设您有一个包含
MyProject
Robolectric测试的TestMyProject
,该文件应包含以下内容。 (The path below is relative to TestMyProject
). (以下路径与
TestMyProject
)。
manifest=../MyProject/AndroidManifest.xml
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.