【问题标题】:TestNG - How can I set up the order of running @Test methods through testng.xml?TestNG - 如何通过 testng.xml 设置运行 @Test 方法的顺序?
【发布时间】:2014-02-08 02:23:03
【问题描述】:

我的 @Test 类中有 30 个 @Test 方法和 2 个 Java 方法。 我需要在具体的@Test 方法之后运行这 2 个 Java 方法,例如, TestMethod5()。 我该怎么做?

例如:

@Test
public void TestMethod5() {

/* compiled code */

}

public void Method1(){/* compiled code */};
public void Method2{/* compiled code */};

需要2个方法:

1) 使用 testng.xml
2) 使用 Intellij IDEA

注意:@BeforeMethod@AfterMethod 仅适用于 扩展 类名 命令。这2个Java方法检查渲染 带有assertTrue() 方法的按钮和标签,所以我不想弄乱我的 基类。


是的,这些方法是测试的一部分(以及未来类似的测试),但我不能只是复制它们的内容并粘贴到我想要的每个方法中......然后@Test方法会被弄乱(大代码).我只需要检查这些方法中标签和按钮的渲染:

 public void method1_ButtonsTest() {

  assertTrue1();
  assertTrue2();

 }


 public void method2_LabelsTest() {

  assertTrue3();
  assertTrue4();
 }


@Test
public void Test1();

@Test
public void Test2();

@Test
public void Test3();

@Test
public void Test4();

@Test
public void Test5();

@Test
public void Test6();


@Test
public void Test7();

@Test
public void Test8();

【问题讨论】:

    标签: java ubuntu intellij-idea testng


    【解决方案1】:

    为什么不能直接调用这些方法?

    @Test public void testMethod5() {
        ...
        method1();
        method2();
    }
    

    【讨论】:

    • 我同意,如果这些方法是更高级别的断言,那么它们就是测试的一部分,就像更简单的 assertXxxx 一样。
    • 是的,这些方法是测试的一部分(以及未来类似的测试),但我不能只是复制它们的内容并粘贴到我想要的每个方法中......然后@Test方法将搞砸了(大代码)。我只需要检查这些方法中标签和按钮的呈现 public void method1() {
    • 我并不是说您应该复制它们的内容,只是说它们是更高级别的断言,因此应该从需要这些断言的每个测试中显式调用。它们可以称为assertLabelRendering()assertButtonRendering()
    • 此外,如果它们不是在测试本身中调用,而是在 @AfterMethod 或等效项中调用,如果它们失败,您可能不会直接知道哪个测试失败,只知道外部断言在一个独立的方法中失败了。
    【解决方案2】:

    如果您不想修改基类并且不想直接在测试中调用方法,那么您可以向测试类添加另一个 @AfterMethod 并只运行您调用的测试用例的方法出来,然后调用基类中的@AfterMethod,类似这样:

    @AfterMethod(alwaysRun = true)
    public void postTestCase(ITestResult _result) {
        if (_result.getMethod().getMethodName().equals("Test1")){
              System.out.println("Do something here...");
        }
        //Call the @AfterMethod in the base class
        super.postTestCase(_result);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-03
      • 1970-01-01
      • 1970-01-01
      • 2018-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多