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

2

6

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.

Northumberland answered 25/10, 2011 at 23:42 Comment(2)
Since you are interested in running your tests in a particular order, it implies that they depend on each other. Your unit tests should be written so that each one is completely independent and the order in which they run does not matter.Roethke
@Noble6 Update: Android Studio to run the testcase goo.gl/ac06C0 and demo to create testcase goo.gl/bQFlmUCreditable
N
7

I finally got it to work using the following command :

adb shell am instrument -e class com.android.foo.LaunchTest -w com.android.foo/android.test.InstrumentationTestRunner
Northumberland answered 27/10, 2011 at 20:5 Comment(1)
What directory did you run this command from, the same directory as LaunchTest?Mceachern
F
0

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
Fainthearted answered 26/10, 2011 at 2:57 Comment(5)
Thanks, I am already using setup() and teardown() but the the thing is LaunchTest launches the app and registers client hence if it is not the first test to execute all other test will fail as the device screen will be hung at registration screen. When I try to use -e class I get an error unable to find instrumentation info.Northumberland
You can only specify one class with "-e class". Maybe you can factor out the registration test and execute the registration during setup()Fainthearted
I am just specifying 1 class but still getting the failure. If don't mention any class, the test executes.Northumberland
@AkhilLatta I updated my answer for this. Also, are you saying that you get an error when you just try to run all tests from the command line?Fainthearted
Thanks for the quick response spatulamania, I have tried to run with just 1 class. When I run adb shell am instrument -w -e class com.android.foo.LaunchTest com.android.foo/android.test.InstrumentationTestRunner I get an error saying Instrumentation not found. But when I do adb shell am instrument -w com.android.foo/android.test.InstrumentationTestRunner, It runs fine without any error.Northumberland

© 2022 - 2024 — McMap. All rights reserved.