方案大纲:使用白名单&范围?

发布于 2025-02-05 12:06:29 字数 1961 浏览 3 评论 0原文

我一直在建立一个黄瓜自动化框架,并有许多要测试的组件。我已经使用了方案大纲来捕获各种值及其预期响应。

我认为的问题: 我必须指定每种类型的数据输入和错误消息。从下面的示例场景概述中,您可以看到我有一定的数字,所有这些都希望返回一条消息。如果有什么不等于这些值,返回错误消息:

Scenario Outline: Number is or is not valid
    Given I send an event with the "Number" set to <num>
    Then I will receive the following <message>
    Examples:
        | num    | message           |                                                                                     
        | 0      | "Processed event" |                                                        
        | 1      | "Processed event" |                                                                  
        | 2      | "Processed event" |                                                                 
        | 3      | "Processed event" |                                                                 
        | 4      | "Processed event" |                                                                 
        | 5      | "Processed event" |                                                                                                                                 
        | 6      | "Message failed"  |
        | -1     | "Message failed"  |
        | "One"  | "Message failed"  |

我想做什么: 我基本上希望在方案概述中定义了一个良好数据的“白名单”,如果有其他值输入 - 它返回了预期的错误消息。像以下内容:

Scenario Outline: Number is or is not valid
    Given I send an event with the "Number" set to <num>
    Then I will receive the following <message>
    Examples:
        | num                |  message          |                                                                                     
        | 0-5                | "Processed event" |                                                        
        | Anything else      | "Message failed"  |  

背后的代码是否可以使用?如您所见,它将具有使自动化套件更加简洁和可维护的好处。如果是这样,请告诉我,渴望讨论。

谢谢!

柯斯蒂

I have been building up a Cucumber automation framework and have a number of components to test. I have used Scenario Outline's to capture various values and their expected responses.

What I see as a problem:
I have to specify every single type of input of data and the error message to go with it. From the example Scenario Outline below you can see I have certain numbers that all are expected to return the one message. If anything does not equal these values the return an error message:

Scenario Outline: Number is or is not valid
    Given I send an event with the "Number" set to <num>
    Then I will receive the following <message>
    Examples:
        | num    | message           |                                                                                     
        | 0      | "Processed event" |                                                        
        | 1      | "Processed event" |                                                                  
        | 2      | "Processed event" |                                                                 
        | 3      | "Processed event" |                                                                 
        | 4      | "Processed event" |                                                                 
        | 5      | "Processed event" |                                                                                                                                 
        | 6      | "Message failed"  |
        | -1     | "Message failed"  |
        | "One"  | "Message failed"  |

What I would like to do:
I would basically like to have a "whitelist" of good data defined in the Scenario Outline and if there is any other value input - it returns the the expected error message. Like the following:

Scenario Outline: Number is or is not valid
    Given I send an event with the "Number" set to <num>
    Then I will receive the following <message>
    Examples:
        | num                |  message          |                                                                                     
        | 0-5                | "Processed event" |                                                        
        | Anything else      | "Message failed"  |  

Is the following possible with the code behind it? As you can see it would have benefits of making an automation suite far more concise and maintainable. If so please let me know, keen to discuss.

Thanks!

Kirsty

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

作妖 2025-02-12 12:06:29

cucumber是支持bdd 的工具。这意味着当您必须交流行为时,它的工作原理非常好。但是,这个特殊的问题正在验证事件验证器的属性,即基于属性的测试。因此,相应地分配测试策略可能值得。

看来有一个规则,即处理有效的事件结果并拒绝无效的事件。这是您可以用黄瓜测试的东西。例如:

Feature: Events
    
    This system accepts events. Events are json messages.
    Examples of well known valid and invalid json messages
    can be found in the supplementary documentation.

    Scenario: The system accepts valid events
      When a well known valid event is send
      Then the system accepts the event
      And responds with "Processed event"

    Scenario: The system rejects invalid events
      When a well known invalid event is send
      Then the system rejects the event
      And responds with "Message failed"

看来有一个规则,即有效事件在0-5之间设置了一个字段“编号”。而且由于听起来像是JSON对象,所以我猜想字符串“ 0”,“ 1”,“ 2”,“ 3”,“ 4”,“ 5”也有效。其他任何事情都是无效的。

详尽测试的一种好方法是使用基于属性的测试框架。例如 jqwik 。给定一组有效或无效值的描述,它将随机尝试一些。对于简化的示例:

package my.example.project;

import net.jqwik.api.*;

import static org.assertj.core.api.Assertions.assertThat;

class ValidatorProperties {

    @Provide
    Arbitrary<Object> validValues() {
        Arbitrary<Integer> validNumbers = Arbitraries.integers().between(0, 5);
        Arbitrary<String> validStrings = validNumbers.map(Object::toString);
        return Arbitraries.oneOf(validNumbers, validStrings);
    }

    @Provide
    Arbitrary<Object> invalidValues() {
        Arbitrary<Integer> invalidNumbers = Arbitraries.oneOf(
                Arbitraries.integers().lessOrEqual(-1),
                Arbitraries.integers().greaterOrEqual(6)
        );
        Arbitrary<String> invalidStrings = invalidNumbers.map(Object::toString);
        return Arbitraries.oneOf(
                invalidNumbers, 
                invalidStrings, 
                Arbitraries.just(null)
        );
    }

    @Property
    void accepts0To5(@ForAll("validValues") Object value) {
        Validator validator = new Validator();
        assertThat(validator.isValid(value)).isTrue();
    }

    @Property
    void rejectsAnythingElse(@ForAll("invalidValues") Object value) {
        Validator validator = new Validator();
        assertThat(validator.isValid(value)).isFalse();
    }

    static class Validator {

        boolean isValid(Object event) {
            return event != null && event.toString().matches("^[0-5]$");
        }

    }

}

以这种方式拆分黄瓜测试,描述了系统应如何响应有效和无效的事件。虽然JQWIK测试描述了有效事件的属性是什么。这使得第二次更加清晰,并且在第二个方面具有更大的保真度。

Cucumber is a tool to support BDD. This means that it works really well when you have to communicate about behavior. But this particular problem is going towards validating the properties of the event validator, i.e. property based testing. So it might be worth to split the test strategy accordingly.

It appears there is a rule that valid events results are processed and invalid events are rejected. This is something you could test with Cucumber. For example:

Feature: Events
    
    This system accepts events. Events are json messages.
    Examples of well known valid and invalid json messages
    can be found in the supplementary documentation.

    Scenario: The system accepts valid events
      When a well known valid event is send
      Then the system accepts the event
      And responds with "Processed event"

    Scenario: The system rejects invalid events
      When a well known invalid event is send
      Then the system rejects the event
      And responds with "Message failed"

It also appears there is a rule that valid events have a field "Number" set to any value between 0-5. And since sounds like a json object I'm guessing the strings "0", "1", "2", "3", "4", "5" are also valid. Anything else is invalid.

A good way to test this exhaustively is by using property based testing framework. For example JQwik. Given a description of a set of either valid or invalid values it will randomly try a few. For a simplified example:

package my.example.project;

import net.jqwik.api.*;

import static org.assertj.core.api.Assertions.assertThat;

class ValidatorProperties {

    @Provide
    Arbitrary<Object> validValues() {
        Arbitrary<Integer> validNumbers = Arbitraries.integers().between(0, 5);
        Arbitrary<String> validStrings = validNumbers.map(Object::toString);
        return Arbitraries.oneOf(validNumbers, validStrings);
    }

    @Provide
    Arbitrary<Object> invalidValues() {
        Arbitrary<Integer> invalidNumbers = Arbitraries.oneOf(
                Arbitraries.integers().lessOrEqual(-1),
                Arbitraries.integers().greaterOrEqual(6)
        );
        Arbitrary<String> invalidStrings = invalidNumbers.map(Object::toString);
        return Arbitraries.oneOf(
                invalidNumbers, 
                invalidStrings, 
                Arbitraries.just(null)
        );
    }

    @Property
    void accepts0To5(@ForAll("validValues") Object value) {
        Validator validator = new Validator();
        assertThat(validator.isValid(value)).isTrue();
    }

    @Property
    void rejectsAnythingElse(@ForAll("invalidValues") Object value) {
        Validator validator = new Validator();
        assertThat(validator.isValid(value)).isFalse();
    }

    static class Validator {

        boolean isValid(Object event) {
            return event != null && event.toString().matches("^[0-5]
quot;);
        }

    }

}

Split this way the Cucumber tests describe how the system should respond to valid and invalid events. While the JQwik test describe what the properties of a valid and invalid event are. This allows much more clarity on the first and a greater fidelity on the second.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文