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

问题描述 投票:0回答:1

我目前有一个工作测试,它验证授权和禁止的API响应(401和403响应)。

我现在想要在现有测试中添加一个成功参数,而不是必须创建另一个单独的测试。但我不确定如何将其纳入其中。基本上我会将响应代码添加为示例的成功(200),但我不确定我将headerCondition设置为什么。另外,为了在我的代码中获得成功(200),我需要传递密钥'token-auth'并且值'ToBeProvided'。我需要在下面粘贴的When步骤中包含它。

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);
    }
c# specflow
1个回答
0
投票

您可以为When步骤添加一个新的boolean参数,或者将您已经拥有的布尔值(技术上,你已经使它成为一个字符串)更改为表示枚举的字符串。

一个布尔值(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作为When步骤的输入而不是headerCondition。

© www.soinside.com 2019 - 2024. All rights reserved.