java - TestNG - 如何在 MultipleTest 类中调用单个测试类

标签 java selenium testing

我开始将 selenium webdriver 与 TestNG 结合使用。我已经创建了一个可以运行多个测试的多测试类,但是,如何在不复制整个代码的情况下调用每个测试 block 中的其他类?

 public WebDriver driver;
  //Test 1
  @Test(priority = 0) //Set Priority of Test - Priority of test always starts from Zero
 public void one() {

  System.out.println("This is Test Case 1");
  }
//Test 2
 @Test(priority = 1) // Test priority 1
 public void Two(){

  System.out.println("This is Test Case 2");
  }

我是否需要创建一个函数来调用每个测试 block 来运行其他类?

最佳答案

按照说明使用setUp() 方法here实例化该类并将其保留为属性。

The method setUp() will be invoked after the test class has been built and before any test method is run.

import org.testng.annotations.*;

public class MyTest {

 private MyService myService;

 @BeforeClass
 public void setUp() {
      myService = new MyService();
 }

 @Test
 public void testSomething() {
      myService.doSomething();
 }
}

关于java - TestNG - 如何在 MultipleTest 类中调用单个测试类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34399072/

相关文章:

python - Django.core.exceptions.ImproperlyConfigured : Error running functional_tests. py

java - 逐步构建对象,同时避免空值

java - 从 Joda DateTime 对象中删除时区

python - 如何在Python中使用BS4从谷歌图像中抓取高分辨率图像

python - 无法从网页获取某些表格数据的内容

ruby-on-rails - Ruby on Rails 2.3.8 : Testing: Instance Variable Nil, 何时在类顶部声明?

java - JVM 是否对 jar 和 war 文件应用任何默认压缩

java - OutOfMemory 异常(不确定)android

c# - 如何使用 Selenium WebDriver C# 在 YouTube 中使用上传按钮

ios - 缺少 XCTest 框架