我在单个类中使用@Test注释进行了5次测试(我正在使用java和TestNG)和一个具有一些逻辑且每个测试方法调用此方法的公共帮助器方法(在同一个类中)。问题是,所有5个测试都在通过,但是testng正在尝试执行辅助方法并将其作为跳过/失败运行。以下是我正在使用的代码:
public class TestClass extends BaseTestClass {
@Test
public void testA(){
//first test code
}
@Test
public void testB(){
//second test code
}
@Test
public void testC(){
//third test code
}
@Test
public void testD(){
//fourth test code
}
@Test
public void testE(){
//fifth test code
}
public void helperMethod( ){
//some logic that each test method is using
}
}
以下是获得的结果:
PASSED: testA
PASSED: testB
PASSED: testC
PASSED: testD
PASSED: testE
SKIPPED: helperMethod
org.testng.TestNGException:
Method helperMethod requires 2 parameters but 0 were supplied in the @Test annotation.
at org.testng.internal.Parameters.checkParameterTypes(Parameters.java:198)
at org.testng.internal.Parameters.createParameters(Parameters.java:134)
at org.testng.internal.Parameters.createParameters(Parameters.java:373)
at org.testng.internal.Parameters.handleParameters(Parameters.java:450)
at org.testng.internal.Invoker.handleParameters(Invoker.java:1383)
at org.testng.internal.Invoker.createParameters(Invoker.java:1075)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1180)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
at org.testng.TestNG.run(TestNG.java:1057)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
===============================================
Default test
Tests run: 6, Failures: 0, Skips: 1
===============================================
但是,如果我将helperMethod声明为private,它可以正常工作。任何人都可以解释一下,为什么会这样?
答案 0 :(得分:10)
您的TestClass
或其任何超类是否有@Test
注释?如果一个类具有这样的注释,则所有公共方法都被视为测试方法。