奇怪问题1:Maven+testng无法识别新添加的类内的测试用例

在Maven+TestNG+HTTPClient接口测试项目中,遇到新创建的名为CompatibilityScore的类内的测试用例无法执行的问题。原因在于Surefire Plugin默认只识别以Test开头或以Test, TestCase结尾的Java文件。通过将文件名改为CompatibilityScoreTest.java,测试用例得以顺利执行。此记录用于备忘。" 50378937,5117519,Bitmap二次采样与内存优化,"['Android开发', '图像处理', '内存管理', 'Bitmap优化']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

项目背景:

Maven+TestNG+HTTPClient 的接口测试项目,测试用例主要依赖TestNG的groups来管理和分组

问题描述:

今天照常进行自动化case的维护,添加了一个新的名为CompatibilityScore的类,并在里面加了几个测试用例,这些测试用例都在同一组里

简单代码如下:

public class CompatibilityScore extends TestSuiteSetup {
	@BeforeClass(alwaysRun = true)
	public void attachTenant() {
		//daima
	}


	@Test(dataProviderClass = DataProviders.class, groups = "api.compatibility.score", dataProvider = "modelIdProvider")
	public void bulkScore(String modelId) {
		//测试逻辑
	}

	@Test(dataProviderClass = DataProviders.class, groups = "api.compatibility.score", dataProvider = "modelIdProvider")
	public void bulkScoreWithEnrich(String modelId) {
		//测试逻辑
	}

	@Test(dataProviderClass = DataProviders.class, groups = "api.compatibility.score", dataProvider = "modelIdProvider")
	public void RTSScore(String modelId) {
		//测试逻辑
	}
}


pom文件主要内容如下:

<plugin>
		<groupId>org.apache.maven.plugins</groupId>
		<artifactId>maven-surefire-plugin</artifactId>
		<version>2.19.1</version>
		<configuration>
			<properties>
				<property>
					<name>usedefaultlisteners</name>
					<value>false</value> <!-- disabling default listeners is optional -->
				</property>
				<property>
					<name>listener</name>
					<value>org.uncommons.reportng.HTMLReporter,
						org.uncommons.reportng.JUnitXMLReporter</value>
				</property>
				<property>
					<name>surefire.testng.verbose</name>
					<value>1</value>
				</property>
			<groups>${groups}</groups>
			<testFailureIgnore>true</testFailureIgnore>
			<parallel>methods</parallel>
			<threadCount>3</threadCount>
		</configuration>
	</plugin>

然后执行 下列命令来执行测试用例

mvn clean test -Dgroups=api.compatibility.score
但是却得到了如下结果

===============================================
Surefire suite
Total tests run: 0, Failures: 0, Skips: 0
===============================================

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 96.453 sec - in TestSuite
竟然没有探测到新的测试用例?!

开启mvn的debug再次执行

mvn -X clean test -Dgroups=api.compatibility.score
看到这么一句

Forking command line: cmd.exe /X /C "C:\Java\jdk1.8.0_112\jre\bin\java -jar PROJECT_PATH\target\sure
fire\surefirebooter4790391759985822523.jar PROJECT_PATH\target\surefire\surefire5239123421203456458tmp PROJECT_PATH\target\surefire\surefire_02709615893746419650tmp"
这就是最终执行的命令,找到对应的路径下用文档编辑工具打开
surefire5239123421203456458tmp
以我的为例,里面主要一段是这样的

#surefire
#Tue Jan 17 15:23:03 CST 2017
tc.5=com.lattice.qa.api.model.ModelMigrationTest
tc.6=com.lattice.qa.api.model.PMMLModelCreationTest
tc.3=com.lattice.qa.api.model.DeDupTest
tc.4=com.lattice.qa.api.model.ModelCreationTest
tc.1=com.lattice.qa.api.correctness.MultipleModelsTest
tc.2=com.lattice.qa.api.integration.End2EndTest
tc.0=com.lattice.qa.api.correctness.EventTableValuesTest
listener=org.uncommons.reportng.HTMLReporter,\n\t\t\t\t\t\t\t\torg.uncommons.reportng.JUnitXMLReporter
threadcount=3
tc.9=com.lattice.qa.api.scoring.RealTimeScoringTest
tc.20=com.lattice.qa.unit.MapsTest
tc.7=com.lattice.qa.api.model.RefineNCloneTest
tc.21=com.lattice.qa.unit.MarketoTest
tc.8=com.lattice.qa.api.scoring.BulkScoringTest
tc.22=com.lattice.qa.unit.TenantConsoleTest
tc.12=com.lattice.qa.api.scoring.ScoringBackwardCompatibilityTest
tc.13=com.lattice.qa.CleanupTenantsTest
tc.14=com.lattice.qa.performance.ScoreingPerfTest
tc.15=com.lattice.qa.sample.basic.BasicTest
tc.16=com.lattice.qa.sample.db.ruid.RUIDSampleTest
parallel=methods
tc.17=com.lattice.qa.TestSuiteSetup
tc.18=com.lattice.qa.unit.AvroParserTest
tc.19=com.lattice.qa.unit.LPRestAPI3Test
groups=api.compatibility.scor
surefire.testng.verbose=1
tc.10=com.lattice.qa.api.scoring.RTSBackwardCompatibilityTest
usedefaultlisteners=false
tc.11=com.lattice.qa.api.scoring.RTSTest
tc.23=com.lattice.qa.unit.TenantsCleanTest
这些以tc开头的属性定义就指定了测试用例所在的类但是看了一遍并没有找到我刚才定义的类CompatibilityScore,这也就是为什么maven执行的时候会找不到测试用例.

那么为什么maven 找不到这个类呢....

再次Google发现是因为 surefire plugin 默认只能识别以Test开头或者Test,TestCase结尾的java文件. 真是够奇葩


By default, the Surefire Plugin will automatically include all test classes with the following wildcard patterns:

  • "*/Test.java" - includes all of its subdirectories and all java filenames that start with "Test".
  • "**/*Test.java" - includes all of its subdirectories and all java filenames that end with "Test".
  • "**/*TestCase.java" - includes all of its subdirectories and all java filenames that end with "TestCase".


再次把文件名改成CompatibilityScoreTest.java 后 顺利执行.


行文寥做备忘.















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值