java - @BeforeClass 方法不在每节课之前运行

标签 java xml selenium testing selenium-webdriver

我有两个类,每个类包含 2 个测试用例和 Test1 类,其中一个方法带有 @BeforeClass,按照我的说法,这个方法也应该在 Test2 类之前运行,但它没有运行。

    package WebPackage;

    import org.testng.annotations.BeforeClass;
    import org.testng.annotations.Test;

    public class Test1 {
        @BeforeClass
        public void test1() {

            System.out.println("printing Before Class Method");
        }
        @Test (priority = 1)
    public void test2() {

            System.out.println("printing test_2");
        }

        @Test (priority = 3)
    public void test3() {

            System.out.println("printing test_3");
        }
    }

测试2

    package WebPackage;

    import org.testng.annotations.Test;

    public class Test2 {

        @Test (priority = 1)
        public void test4() {

                System.out.println("printing test_4");
            }

            @Test (priority = 3)
        public void test5() {

                System.out.println("printing test_5");
            }
    }

XML文件


    <!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
    <suite name="Menu">
      <test name="WebPackage">
        <classes>
          <class name="WebPackage.Test1"/>
         <class name="WebPackage.Test2"/>
        </classes>
      </test> <!-- Test -->
    </suite> <!-- Suite -->

控制台

[RemoteTestNG] detected TestNG version 7.0.0
printing Before Class Method
printing test_2
printing test_3
printing test_4
printing test_5

===============================================
Menu
Total tests run: 4, Passes: 4, Failures: 0, Skips: 0
===============================================

最佳答案

您可以使用 @BeforeClass 注释创建一个 BaseTest 类,然后每个测试类都使用 BaseTest 进行扩展。

基础测试:

public class BaseTest {
    @BeforeClass
    public void test1() {
        System.out.println("printing Before Class Method");
    }
}

测试 1:

public class Test1 extends BaseTest {
    @Test (priority = 1)
    public void test2() {
        System.out.println("printing test_2");
    }

    @Test(priority = 3)
    public void test3() {
        System.out.println("printing test_3");
    }
}

测试 2:

public class Test2 extends BaseTest {
    @Test(priority = 1)
    public void test4() {
        System.out.println("printing test_4");
    }

    @Test (priority = 3)
    public void test5() {
        System.out.println("printing test_5");
    }
}

输出:

printing Before Class Method
printing test_2
printing test_3
printing Before Class Method
printing test_4
printing test_5
===============================================
Menu
Total tests run: 4, Passes: 4, Failures: 0, Skips: 0
===============================================

关于java - @BeforeClass 方法不在每节课之前运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58746286/

相关文章:

java - request-filter-servlet-jsp 链中的响应

java - 即使抛出 thread.sleep 异常后也会出错

java - 如何仅使用 Java (JavaConfig) 实现 Spring 集成 Content Enricher?

xml - Grails/Groovy - 将对象呈现/直接转换为 XML 以保存到文件

c# - 尝试使用 XDocument 和 XmlSchemaSet 从 xml 模式在 C# 中创建 xml

python - 为什么 Selenium 的 wait.until_not(EC.invisibility_of_element_ located) 等待加载器消失的时间太长?

ruby-on-rails - 使用 Rails 4.x 安装 RSpec、Capybara 和 Selenium Webdriver

java - 使用 Hibernate 实体的域实体?

json - 有没有像 csv 或 json 这样的东西,但更图形化,更适合人类阅读?

c# - Firefox WebDriver (Selenium) 和安全警告弹出窗口