通用技术 [提问] 关于 testng 的 beforeMethod 标签化问题

dengwei729 · November 01, 2016 · Last by dengwei729 replied at November 01, 2016 · 1633 hits

【背景】
随着用例数目增多,不同类型的用例需要的初始化步骤不同,想通过 groups 配合 beforeMethod 来区分初始化工作。
不想通过类来管理,用例组织不太好看。--- 大家暂且忽略这个方案
尝试对 beforeMethod 标签化,未能生效,群里大牛帮忙答疑~~~ 😁😁😁😁

【代码】

// test case 1
@Test(groups = {"reset"})
public void testCase1() {
    System.out.println("in test case 1");
}

// test case 2
@Test
public void testCase2() {
    System.out.println("in test case 2");
}

@BeforeMethod(groups = {"reset"}, alwaysRun = false)
public void beforeMethod1() {
    System.out.println("in beforeMethod1-reset");
}

@BeforeMethod
public void beforeMethod2() {
    System.out.println("in beforeMethod2");
}

【期望结果】
testCase1 调用 beforeMethod1 初始化
testCase2 调用 beforeMethod2 初始化

【实际结果】

in beforeMethod1-reset
in beforeMethod2
in test case 1
in beforeMethod1-reset
in beforeMethod2
in test case 2

两个 beforeMethod 均被用例调用,未能达到期望的效果

共收到 8 条回复 时间 点赞

dependsOnMethods 用这个标签

java.lang.String[] groups
The list of groups this class/method belongs to.

#1 楼 @yzx200712256 想依照 group 配置,method 太多了

#2 楼 @jet 能说具体一些么?

#5 楼 @jet 奥,抱歉,改成字符串数组后,依旧木有生效,文章代码已修改
尝试 dependsOnGroups 也没有生效。

#7 楼 @jet 这个对 group 是会执行一次,我一个 group 里头会有多条用例,需要执行多次

需要 Sign In 后方可回复, 如果你还没有账号请点击这里 Sign Up