【问题标题】:TestNG skips subsequent runs of @Test(invocationCount = 3) if @AfterMethod fails如果 @AfterMethod 失败,TestNG 会跳过后续的 @Test(invocationCount = 3) 运行
【发布时间】:2020-01-13 19:34:04
【问题描述】:

使用 testNG 运行此代码:

package org.example;

import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class Test1 {

    @BeforeMethod(alwaysRun = true)
    public void before() throws Exception {
        System.out.println("BEFORE, thread ID=" + Thread.currentThread().getId());
    }

    @AfterMethod(alwaysRun = true)
    public void after() throws Exception {
        System.out.println("AFTER, thread ID=" + Thread.currentThread().getId());
        throw new Exception("Error!");
    }

    @Test(alwaysRun = true, invocationCount = 3, threadPoolSize = 1)
    public void test() throws Exception {
        System.out.println("TEST, thread ID=" + Thread.currentThread().getId());
    }

}

返回

BEFORE, thread ID=1
TEST, thread ID=1
AFTER, thread ID=1
BEFORE, thread ID=1
AFTER, thread ID=1
BEFORE, thread ID=1
AFTER, thread ID=1

@Test 方法仅第一次运行,之后被跳过。如何实现这一点,即避免跳过 @Test 方法:

BEFORE, thread ID=1
TEST, thread ID=1
AFTER, thread ID=1
BEFORE, thread ID=1
TEST, thread ID=1
AFTER, thread ID=1
BEFORE, thread ID=1
TEST, thread ID=1
AFTER, thread ID=1

【问题讨论】:

    标签: java testng


    【解决方案1】:

    结果显示测试是按顺序运行的,而不是并行运行的。 因此,单个线程被用于运行测试用例的所有 3 次调用。

    如果调用需要并行运行,则必须在 testng.xml 文件中设置以下参数:

    1. parallel="methods"
    2. thread-count="3"

    并行运行的工作示例 testng.xml 文件:

    <suite name="Run tests in parallel" parallel="methods" thread-count="3">
        <test name="3-threads-in-parallel">
            <classes>
                <class name="com.company.tests.TestClass"/>
            </classes>
        </test>
    </suite>
    

    【讨论】:

    • 不,我确实不想并行运行,因为测试通过串行端口与设备通信。顺序很好,重要的是在第 2 次和第 3 次运行中没有 test() 调用。
    • 结果显示测试实际上已经运行了3次。一次一个线程。 '1' 的线程 ID 被打印 3 次表明测试仅在线程 #1 上运行了 3 次。
    • 仔细看看我最初的帖子:@Test 方法只运行第一次。我想让它运行 3 次。
    猜你喜欢
    • 2015-10-07
    • 1970-01-01
    • 1970-01-01
    • 2020-01-07
    • 1970-01-01
    • 2012-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多