[英]Run Junit test using android instrumentation on a package with classes in specific order
我正在嘗試使用命令行運行android instrumentation Junit測試。我正在使用以下命令,它正在啟動測試權限。
adb shell am instrument -w com.android.foo/android.test.InstrumentationTestRunner
我的android項目包有以下java源文件(按字母順序)
com.android.foo
ActivityTest
ContactsTest
LaunchTest
SendTest
當我使用上面提到的命令運行測試時,測試首先開始執行ActivityTest,依此類推。 這不是我想要的,我希望它首先執行LaunchTest,然后是ContactTest,SendTest和ActivityTest。 我試過用
adb shell am instrument -w -e class com.android.foo.LaunchTest,com.android.foo.ContactTest com.android.foo/android.test.InstrumentationTestRunner
但它給我一個錯誤可能是因為我沒有在我的代碼中使用TestCase類,而是我的LaunchTest和其他人擴展了ActivityInstrumentationTestCase2。
任何幫助表示贊賞。
我終於使用以下命令使其工作:
adb shell am instrument -e class com.android.foo.LaunchTest -w com.android.foo/android.test.InstrumentationTestRunner
如果測試執行的順序很重要,那么測試很脆弱,應該重構。 這意味着它們相互依賴,理想情況下,測試是獨立的。 通常大多數測試是如此獨立和細粒度,我們稱之為單元測試。
打破這種依賴的一個常見開端是在TestCase中使用setup()
和teardown()
方法。 在這里,您可以准備測試以運行和清除測試可能發生的任何更改。
話雖這么說, android.test.InstrumentationTestRunner
沒有用於修改測試套件的選項。 然而,這可以通過兩種方式完成。
1)您可以創建自己的android.test.InstrumentationTestRunner
實現,它執行一些特殊的排序。 這將為您提供最大的靈活性,但可能需要更多時間。
2) am instrument
可以將類名作為參數,因此您可以按順序運行測試但運行多個命令(可能在bash腳本中組合)。 這是通過添加參數“-e class [classname of test]”來完成的。
此外,您運行測試的方式有誤:
adb shell am instrument -w -e class com.android.foo.LaunchTest,com.android.foo.ContactTest com.android.foo/android.test.InstrumentationTestRunner
試圖運行兩個類。 要執行此操作,您需要將其更改為:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.