【问题标题】:Reversible resource modification in JavaJava中的可逆资源修改
【发布时间】:2018-05-08 14:20:45
【问题描述】:

因此,有一个庞大的 java 测试框架项目与各种硬件组件一起工作。问题是:在@Beforemethod 中出现异常/硬件故障等情况时,@Aftermethod 无法动态地决定将哪些资源设置回其相应的原始状态/值。这可能会危及依赖相同硬件元素状态的后续测试用例(主要是假阴性)。

我想分别在遇到错误之前反转@BeforeMethod中发生的所有对象修改。这样我可以使其他测试更容易出错(得到假阴性的机会更少)。

为每个套件定义一个原子状态不是一种选择(在我看来) - 太麻烦了,需要大量的代码修改,因此为每个对象设置原子状态可能需要比应有的更多时间。

有什么建议吗?您知道针对此类问题的任何好的测试指南/模式吗?

编辑:

TestClass1{

@BeforeMethod
method(){
    resource1.setfoo("foo");
    resource1.setbar("bar"); 
    ...
    resource7.setfoo("bar"); // -> hw error occurs, testmethod1 is not run
    ...
}

testmethod1(){
    foo.bar();
}

}


TestClass2{

testmethod2(){
    assertTrue(resource1.doSomething()); /*fails because some combination of 
    the resource modifications that happened in the previous @Beforemethod 
    in TestClass1 changed the hardware operation in some way. */
}

}

【问题讨论】:

  • 为什么不能在 @Beforemethod 本身中处理这些问题?
  • 您能帮忙编辑您的问题并在帖子中添加一个示例,以显示您在说什么吗?

标签: java testing junit testng


【解决方案1】:

不需要处理原子状态,只要有确定的方法来反转资源的状态即可。例如,在 @BeforeMethod 中添加一个计数器和一个 try/catch 块就足够了:

@BeforeMethod
void method(){
    int setupStep = 0;
    try {
        resource1.setfoo("foo");
        setupStep++; //1
        resource1.setbar("bar");
        setupStep++; //2
        ...
        resource7.setfoo("bar"); // -> hw error occurs, testmethod1 is not run
        setupStep++; //99
        ...
    } catch (Exception e) {
        switch (setupStep) {
            case 99:
                resource7.setfoo("bar_orig");
            ...
            case 2:
                resource1.setbar("bar_orig");
            case 1:
                resource1.setfoo("foo_orig");
            default:
                // Failed on first step
        }
        throw e; //Make sure the set-up method fails
    }
}

注意switch 块的跌落功能的使用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-04
    • 2012-11-07
    • 1970-01-01
    • 2011-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多