如何在我现有的Specflow测试中添加另一个参数?

时间:2019-04-15 08:16:43

标签: c# specflow

我目前有一个工作测试,可以对授权和禁止的API响应(401和403响应)进行身份验证。

我现在想在现有测试中添加成功参数,而不必创建另一个单独的测试。但是我不确定如何将其合并。因此,从本质上讲,我将在示例中添加成功的响应代码(200),但不确定将headerCondition设置为什么。另外,要在我的代码中获得成功(200),我需要传递密钥'token-auth'和值'ToBeProvided'。我需要将其包括在下面的“何时”步骤中。

Scenario Outline: Authenticating endpoint
When I request transaction notification endpoint with headers 
<HeaderCondition>
Then I get a response <ResponseCode>
Examples:
| ResponseCode | HeaderCondition |
| Unauthorized | false           |
| Forbidden    | true            |

[When(@"I request notification endpoint with headers (.*)")]
      public void 
WhenIRequestNotificationEndpointWithHeaders(string headerCondition)
    {
        var baseurl = "(My end point)";
        var client = new RestClient(baseurl);
        var request = new RestRequest(Method.PUT);

        if (headerCondition.Equals("true"))
        {
            request.AddHeader("Ocp-Apim-Subscription-Key", "b601454182cf47eba7ahfjuejdksiwhfjmd");
            request.AddHeader("Content-Type", "application/json");
            //request.AddJsonBody("{\"Id\":\"123\"}");
            request.AddParameter("undefined", "{\"Id\":\"123\"}", ParameterType.RequestBody);
        }

        response = client.Execute(request);
    }

1 个答案:

答案 0 :(得分:0)

您可以为“何时”步骤添加新的布尔参数,也可以将已经拥有的布尔值(从技术上讲,已使其成为字符串)更改为表示枚举的字符串。

再加上一个布尔(IsSendingToken)的示例:

Scenario Outline: Authenticating endpoint
When I request transaction notification endpoint with headers <HeaderCondition> and sending token <IsSendingToken>
Then I get a response <ResponseCode>

Examples:
| ResponseCode | HeaderCondition | IsSendingToken |
| Unauthorized | false           | false          |
| Forbidden    | true            | false          |
| Ok           | true            | true           |

[When(@"I request notification endpoint with headers (.*) and sending token (true|false)")]
public void WhenIRequestNotificationEndpointWithHeaders(string headerCondition, bool isSendingToken)
    {
        var baseurl = "(My end point)";
        var client = new RestClient(baseurl);
        var request = new RestRequest(Method.PUT);

        if (headerCondition.Equals("true"))
        {
            request.AddHeader("Ocp-Apim-Subscription-Key", "b601454182cf47eba7ahfjuejdksiwhfjmd");
            request.AddHeader("Content-Type", "application/json");
            //request.AddJsonBody("{\"Id\":\"123\"}");
            request.AddParameter("undefined", "{\"Id\":\"123\"}", ParameterType.RequestBody);
        }

        if (isSendingToken == true)
        {
            request.AddHeader("token-auth", "ToBeProvided");
        }

        response = client.Execute(request);
    }

将HeaderCondition更改为枚举(作为字符串)的示例:

Scenario Outline: Authenticating endpoint
When I request transaction notification endpoint with headers <HeaderCondition>
Then I get a response <ResponseCode>

Examples:
| ResponseCode | HeaderCondition |
| Unauthorized | Unauthorized    |
| Forbidden    | Forbidden       |
| Ok           | Ok              |

[When(@"I request notification endpoint with headers (.*)")]
public void WhenIRequestNotificationEndpointWithHeaders(string headerCondition)
    {
        var baseurl = "(My end point)";
        var client = new RestClient(baseurl);
        var request = new RestRequest(Method.PUT);

        if (headerCondition.Equals("Forbidden"))
        {
            request.AddHeader("Ocp-Apim-Subscription-Key", "b601454182cf47eba7ahfjuejdksiwhfjmd");
            request.AddHeader("Content-Type", "application/json");
            //request.AddJsonBody("{\"Id\":\"123\"}");
            request.AddParameter("undefined", "{\"Id\":\"123\"}", ParameterType.RequestBody);
        }
        else if(headerCondition.Equals("Ok"))
        {
            request.AddHeader("Ocp-Apim-Subscription-Key", "b601454182cf47eba7ahfjuejdksiwhfjmd");
            request.AddHeader("Content-Type", "application/json");
            //request.AddJsonBody("{\"Id\":\"123\"}");
            request.AddParameter("undefined", "{\"Id\":\"123\"}", ParameterType.RequestBody);
            request.AddHeader("token-auth", "ToBeProvided");
        }

        response = client.Execute(request);
    }

我为HeaderCondition重用了ResponseCode值。 HeaderCondition可以是您喜欢的任何东西,只要它们在功能文件和步骤文件中匹配即可。您还可以将ResponseCode用作While步骤的输入,而不是headerCondition。