量角器/茉莉花有条件的测试用例 [英] Protractor/Jasmine Conditional Test Cases

查看:60
本文介绍了量角器/茉莉花有条件的测试用例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关此问题:如何创建条件测试使用量角器的案例? –我很好奇是否有针对这些情况的合法(有文档证明)答案,因为我无法直接获得答案.

Related to this question: How can I create conditional test cases using Protractor? -- I'm curious whether there is a legitimate (documented) answer to these scenarios, because I can't get a straight answer.

虽然在链接的问题中发布的ignore解决方案有效,但从风格上讲,我不喜欢它.乍一看,您似乎正在忽略/跳过该规范.

While the ignore solution posted in the linked question works, stylistically I'm not a fan of it. At first glance it just looks like you're ignoring/skipping the spec.

此外,我在 Gitter 上问了这个问题-以下代码是不正确的做法吗?

Additionally, I asked this question on Gitter - Is the following code bad practice?

if(questionAnswer == "Yes") {
   it('should display another question', function() {
       // code
   });
}

我从量角器团队的某个人那里得到的关于Gitter的答案相当模糊:

The answer I received on Gitter from someone on the Protractor team was rather vague:

这可能会导致测试结果不稳定...我认为没有什么可以说是不错的做法.如果它适合您,请运行它.

this might lead to flaky tests... I don’t think there’s anything that says it is not bad practice. If it is working for you, then run with it.

我对这个答案不满意,因为他从可能会变得片状"开始...这对我来说听起来不稳定.我看到的唯一替代方法是在规范内正常创建条件,并创建任意断言以捕获else场景,即:

I'm not satisfied with that answer because he started with "might be flaky"... which doesn't sound stable to me. The only alternative I see is to create the conditional inside the spec as normal, and create an arbitrary assertion to capture the else scenario, i.e.:

it('should display another question', function() {
    if(questionAnswer == "Yes") {
        expect(question2.isDisplayed()).toBe(true);
    }
    else {
        expect(true).toBe(true);
    }
});

但是当我只需要50%的时间时,我就会自动添加一个额外的测试用例.我知道这是一个小问题,但确实困扰我.

But then I'm automatically adding an additional test case, when it's only needed 50% of the time. I know it's a small issue but it really bothers me.

上面的代码是我当前面临的情况-如果最后一个规范回答是",则我需要针对下一个问题运行其他规范.如果没有,那就是我测试的终点.真的没有官方方法有条件地在Jasmine/Protractor中运行规范吗?

The above code is the scenario I'm currently facing - If the last spec answered "Yes", I need to run an additional spec for the next question. If not, that's the end of my tests. Is there really no official way to conditionally run a spec in Jasmine/Protractor?

推荐答案

在这种情况下,我使用所谓的 context .通常,上下文用于表示状态更改,该状态更改会影响您正在测试的代码的行为.

In these situations I use what's called a context. Typically a context is used to denote a state change that affects the behavior of the code you're testing.

尽管Jasmine中没有明确提供这些功能,但它们确实存在于其他BDD风格的测试框架中,例如Rspec(

While not explicitly available in Jasmine, they do exist in other BDD-style testing frameworks like Rspec (related reference). Often, context is just an alias for describe.

因此在Jasmine中,我将使用describe并按如下方式构建测试:

So in Jasmine, I will use describe and structure my test as follows:

describe('someMethod', function() {
    describe('when a privileged account', function() {
        beforeEach(function() {
           questionAnswer = "Yes";
           someMethod();
        });

        it('should do something', function() {
            // expectation
        }
    });

    describe('when not a privileged account', function() {
        beforeEach(function() {
           questionAnswer = "No";
           someMethod();
        });

        it('should do something else', function() {
            // expectation
        }
    });
);

我避免条件测试".我宁愿运行更多测试以确保我用尽了所有代码路径.另外,我发现测试更具可读性,这是BDD风格测试的目标之一.

I avoid "conditional tests". I'd rather run more tests to ensure I've exhausted all the code paths. In addition, I find the tests are more readable which is one of the goals of BDD-style testing.

最后,在测试中添加逻辑是人们选择荒谬的测试方式的原因之一.然后测试您的测试以测试测试.然后...

Finally, adding logic to tests is one reason why people go down the absurd path of testing your tests. Then testing your tests that test the tests. and then...

这篇关于量角器/茉莉花有条件的测试用例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆