简体   繁体   中英

Run Junit test using android instrumentation on a package with classes in specific order

I am trying to run android instrumentation Junit test using command line.I am using following command and it is launching the test right.

adb shell am instrument -w com.android.foo/android.test.InstrumentationTestRunner

My android project package has following java source files (in alphabetical order)

com.android.foo

ActivityTest

ContactsTest

LaunchTest

SendTest

When I run the test using the above mentioned command, the test starts executing ActivityTest first and so on. This is not what I want, I want it to execute LaunchTest first followed by ContactTest, SendTest and ActivityTest. I tried using

adb shell am instrument -w -e class com.android.foo.LaunchTest,com.android.foo.ContactTest com.android.foo/android.test.InstrumentationTestRunner

but it gives me an error may be because I am not using TestCase class in my code but instead my LaunchTest and others extends ActivityInstrumentationTestCase2.

any help is appreciated.

我终于使用以下命令使其工作:

adb shell am instrument -e class com.android.foo.LaunchTest -w com.android.foo/android.test.InstrumentationTestRunner

If the order in which your tests execute matters, your tests are brittle and should be refactored. It means that they depend on each other and ideally, tests are independent. Usually most tests are so independent and fine grained that we call them Unit Tests.

A common start for breaking this kind of dependency is to use the setup() and teardown() methods in your TestCase. Here you can prepare for your tests to run and cleanup any changes that your tests might make.


That being said, the android.test.InstrumentationTestRunner does not have an option for reording your test suites. This can however, be done two ways.

1) you can create your own implementation of android.test.InstrumentationTestRunner that does some special ordering. This will give you the most flexibility but may take more time.

2) am instrument can take the class name as an argument so you can run your tests in order but running multiple commands (possibly combined in a bash script). This is done by adding the arguments "-e class [classname of test]".


In addition, there is an error in the way you're running your tests:

adb shell am instrument -w -e class com.android.foo.LaunchTest,com.android.foo.ContactTest com.android.foo/android.test.InstrumentationTestRunner

tries to run for two classes. To do this you need to change it to this:

adb shell am instrument -w -e class com.android.foo.LaunchTest com.android.foo/android.test.InstrumentationTestRunner
adb shell am instrument -w -e class com.android.foo.ContactTest com.android.foo/android.test.InstrumentationTestRunner

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM