问题描述
我试图从命令行运行一个Android测试用例.
I am trying to run just a single Android test case from the command line.
在IDE中,我可以右键单击并运行,但是在CLI中,以下操作将失败:
From the IDE I can just right click and run, but from CLI with the following it fails:
./gradlew test --tests "com.xyz.b.module.TestClass.testToRun"
错误:
> Unknown command-line option '--tests'.
如何运行单个UNIT TEST方法?我想强调的是,我想运行单个单元测试,而不是从命令行运行仪表测试.
How can I run a single UNIT TEST method? I want to emphasis that I want to run a single unit test, not an instrumentation test from command line.
更新:我有一个摄像头应用.想象一下,我有一个名为usCameraDebug
的构建变体. (这意味着美国相机调试)现在您能告诉我如何运行一个称为mySingleTest
的测试用例吗?
Update: I have a camera app. Imagine that I have a build variant called usCameraDebug
. (That means united states camera debug) Now can you tell me how to run a single test case i called mySingleTest
?
正如您提到的,我尝试过此操作:./gradlew test --tests "*mySingleTest"
I tried this as you mentioned: ./gradlew test --tests "*mySingleTest"
和./gradlew app:usCameraDebug test --tests "*mySingleTest"
,还有:./gradlew app:usCameraDebugUnitTest --tests "*mySingleTest"
但它 .不起作用.凯恩,您告诉我要根据我的构建变型确切输入什么.默认情况下,其位于名为"app"的模块中.
and also: ./gradlew app:usCameraDebugUnitTest --tests "*mySingleTest"
but it . does not work. caan you tell me exactly what to type based on my build variant. its in a module called "app" as defaulted.
这是我要运行的测试:
package com.xyz.cameras.parts
@Test
fun mySingleTest(){
assertEquals(12,13)
}
推荐答案
在查找测试名称时,您需要在模式中指定一个通配符,并确保使用模块+样式. --tests
不适用于./gradlew test
或./gradlew check
You need to specify a wildcard in the pattern while looking for the test name, and make sure to use module + flavor. --tests
will not work with ./gradlew test
or ./gradlew check
尝试此模式-> ./gradlew :<module>:<flavor> --tests "*textThatTestNameContains*"
示例->./gradlew :profile:testDebug --tests "*my_profile*"
将运行此测试:
Example ->./gradlew :profile:testDebug --tests "*my_profile*"
will run this test:
@Test
public void my_profile_pageview()
此外,使用--info
标志运行有助于查看测试本身或--debug
以获得更多输出.例如./gradlew --info :profile:testDebug --tests "*my_profile*"
Additionally, running with --info
flag helps to see the tests themselves or --debug
for more output. e.g../gradlew --info :profile:testDebug --tests "*my_profile*"
这篇关于如何针对特定的Gradle构建风格从命令行运行单个单元测试方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!