eclipse 使用 TestNG 和 Java 从另一个类调用方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29613039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calling a method from another class using TestNG and Java
提问by Max Smith
Getting an error below after running test2.java running as TestNG if I run it as Java Application everything is working. What does the error mean and can anyone help me with this using TestNG and Java. I am using Webdriver and Eclipse and just want to do this simple trick to apply for my test scripts.
如果我将它作为 Java 应用程序运行,那么在运行作为 TestNG 运行的 test2.java 后出现以下错误,一切正常。错误是什么意思,任何人都可以使用 TestNG 和 Java 帮助我解决这个问题。我正在使用 Webdriver 和 Eclipse,只是想用这个简单的技巧来应用我的测试脚本。
SKIPPED: main org.testng.TestNGException: Method main requires 1 parameters but 0 were supplied in the @Test annotation.
跳过:main org.testng.TestNGException:方法 main 需要 1 个参数,但在 @Test 注释中提供了 0 个参数。
Here is my code for Test1.java
这是我的 Test1.java 代码
package firsttestngpackage;
import org.testng.annotations.Test;
@Test
public class Test1{
public void message(){
System.out.println("Test");
}
}
Here is my code for Test2.java
这是我的 Test2.java 代码
package firsttestngpackage;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
@Test
public class Test2{
public static void main(String[] args){
Test1 class1 = new Test1();
class1.message();
}
}
回答by vins
If you use @Test
in the class level, all the public methods are considered test methods. You need to supply parameters
to the main method as it expects an argument.
如果@Test
在类级别使用,则所有公共方法都被视为测试方法。您需要提供parameters
给 main 方法,因为它需要一个参数。
Just change it to public void main()
只需将其更改为 public void main()
回答by Jaroslav Cincera
It looks you are using TestNG in wrong way. You don't need java main method. Just use annotation @Test and then run class with TestNG (org.testng.TestNG
) and defined .xml test suite as Java Application or in Eclipse runner. TestNG invokes all tests methods so you don't have to call it manually.
看起来您以错误的方式使用 TestNG。您不需要 java main 方法。只需使用注释@Test,然后使用 TestNG ( org.testng.TestNG
)运行类,并将 .xml 测试套件定义为 Java 应用程序或在 Eclipse 运行器中。TestNG 调用所有测试方法,因此您不必手动调用它。
回答by Max Smith
i got it to work by removing "String[] args"
我通过删除“String[] args”让它工作
package firsttestngpackage;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
@Test
public class Test2{
public void main(){
Test1 class1 = new Test1();
class1.message();
}
}
回答by Sudharshan Ramaiah
jaroslav is right.you are using @Test annotation in a wrong way.While using testNG framework u r not using the main method to run the test.u running it as TestNG tests
jaroslav 是对的。您以错误的方式使用 @Test 注释。虽然使用 testNG 框架,但您没有使用 main 方法来运行 test.u 将其作为 TestNG 测试运行
回答by Sanchit
Firstly the above comments are right. Don't use main method if you are using testNG annotations. Main method can only be used in @Beforeclass
and @Afterclass
. Also no need of (String[] args)
.
首先上面的评论是对的。如果您使用的是 testNG 注释,请不要使用 main 方法。Main 方法只能在@Beforeclass
and 中使用@Afterclass
。也不需要(String[] args)
。
If you are still facing problem with TestNg,
如果您仍然面临 TestNg 的问题,
Make sure you have installed it correctly.
Use this code before your 1st test case (
@Test
)
确保您已正确安装它。
在第一个测试用例 (
@Test
)之前使用此代码
@BeforeTest
Let me know if this helps.
public void setup ()
{
System.setProperty("webdriver.chrome.driver", "*location of your driver*");
ChromeOptions options = new ChromeOptions();
options.addArguments("test-type");
options.addArguments("start-maximized");
options.addArguments("--js-flags=--expose-gc");
options.addArguments("--enable-precise-memory-info");
options.addArguments("--disable-popup-blocking");
options.addArguments("--disable-default-apps");
options.addArguments("test-type=browser");
options.addArguments("disable-infobars");
driver = new ChromeDriver(options);
driver.manage().deleteAllCookies();
}
@BeforeTest
如果这有帮助,请告诉我。
public void setup ()
{
System.setProperty("webdriver.chrome.driver", "*location of your driver*");
ChromeOptions options = new ChromeOptions();
options.addArguments("test-type");
options.addArguments("start-maximized");
options.addArguments("--js-flags=--expose-gc");
options.addArguments("--enable-precise-memory-info");
options.addArguments("--disable-popup-blocking");
options.addArguments("--disable-default-apps");
options.addArguments("test-type=browser");
options.addArguments("disable-infobars");
driver = new ChromeDriver(options);
driver.manage().deleteAllCookies();
}