TestNG新手运行测试遇[TestNG] No tests found问题求助
解决TestNG提示“No tests found. Nothing was run”的问题
我是TestNG新手,最近写了一段包含3个测试方法的代码,故意让test2()和test3()执行失败,但在IDE中运行代码时,控制台一直输出“[TestNG] No tests found. Nothing was run”,我没有使用Maven命令行执行。
附上项目结构截图:
以下是我的TestNG代码:
package priority; import org.testng.Assert; import org.testng.annotations.Test; public class TestNGRetryFailed { @Test public void test1() { System.out.println("i am insde test 1"); } @Test public void test2() { System.out.println("i am insde test 2"); int i=1/0; } @Test public void test3() { System.out.println("i am insde test 3"); Assert.assertTrue(0>1); } }
控制台输出信息如下:
Default suite Total tests run: 0, Failures: 0, Skips: 0 ===============================================[TestNG] No tests found. Nothing was run Usage: [options] The XML suite files to run Options: -configfailurepolicy Configuration failure policy (skip or continue) -d Output directory -dataproviderthreadcount Number of threads to use when running data providers -excludegroups Comma-separated list of group names to exclude -groups Comma-separated list of group names to be run -junit JUnit mode Default: false -listener List of .class files or list of class names implementing ITestListener or ISuiteListener -methods Comma separated of test methods Default: [] -methodselectors List of .class files or list of class names implementing IMethodSelector -mixed Mixed mode - autodetect the type of current test and run it with appropriate runner Default: false -objectfactory List of .class files or list of class names implementing ITestRunnerFactory -parallel Parallel mode (methods, tests or classes) Possible Values: [tests, methods, classes, instances, none, true, false] -port The port -reporter Extended configuration for custom report listener -suitename Default name of test suite, if not specified in suite definition file or source code -suitethreadpoolsize Size of the thread pool to use to run suites Default: 1 -testclass The list of test classes -testjar A jar file containing the tests -testname Default name of test, if not specified in suitedefinition file or source code -testnames The list of test names to run -testrunfactory, -testRunFactory The factory used to create tests -threadcount Number of threads to use when running tests in parallel -usedefaultlisteners Whether to use the default listeners Default: true -log, -verbose Level of verbosity -xmlpathinjar The full path to the xml file inside the jar file (only valid if -testjar was specified) Default: testng.xml
我来帮你排查这个问题,出现No tests found的提示,本质是TestNG没识别到要执行的测试内容,下面是几个常见的排查方向和解决办法:
- 检查IDE的TestNG环境:先确认你的IDE(不管是IntelliJ还是Eclipse)已经装好了TestNG插件,而且项目里已经正确引入了TestNG的依赖——不管是用Maven/Gradle管理,还是手动添加的jar包,一定要确保它在项目的类路径里。
- 确认运行配置是否正确:很多新手会犯的错是把测试类当成普通Java类来运行(比如右键选了
Run 'TestNGRetryFailed.main()'),但你的类里没有main方法啊!正确的做法是右键测试类,选择Run 'TestNGRetryFailed'(IDE会识别这是TestNG测试类,自动用TestNG runner来执行)。 - 检查测试源目录标记:如果你的测试类所在的目录没被IDE标记为“测试源目录”,TestNG也找不到它。比如在IntelliJ里,测试目录应该是蓝色的,要是不对,你可以右键目录→
Mark Directory as→Test Sources Root。 - 手动创建testng.xml(推荐):如果IDE自动识别有问题,咱们手动指定测试类就稳了。创建一个
testng.xml文件放在项目根目录,内容如下:
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" > <suite name="My Test Suite"> <test name="Failed Tests Demo"> <classes> <class name="priority.TestNGRetryFailed"/> </classes> </test> </suite>
然后右键这个xml文件,选择Run 'testng.xml',TestNG就会乖乖按照配置执行你的三个测试方法了。
内容的提问来源于stack exchange,提问作者Fayaz
相关产品推荐
相关产品推荐

