私は現在、SpecFlowを使用することを学んでおり、ユーザーを登録するためのテストを含むFeatureを書きたいと考えています。SpecFlowのバックグラウンドステップを実装する正しい方法
私は最初のテストケース「有効なユーザーを登録する」を持っています。 これには、ユーザー名、電子メール、パスワード、および確認パスワードの入力が含まれます。提出をクリックすると、ユーザーを登録してDBに追加する必要があります。
私の次のテストケースでは、上記の手順を実行してから、同じユーザー名でユーザーを登録しようとすると、エラーが返されます。 私が見つけた例からわかるように、私の.featureファイルで "Background"を使用して、.featureファイルの各シナリオの前に実行したいステップを指定することができます。これには、「バックグラウンド」セクションにユーザーを登録し、「有効な登録ユーザー」のケースで作成されたメソッドを呼び出すためのステップを作成する必要があります。 このquestionは少し助けてくれましたが、利用可能なタグとフックの正しい使い方を理解できないため、私にはさらに多くの質問がありました。
私の質問は以下のとおりです。
- 有効なテストケースはシナリオの残りの部分と同じファイルにあるべきか?そのフィーチャファイル内のすべてのシナリオに対してバックグラウンドセクションが呼び出された場合、有効なテストが失敗する(本質的に有効なテストケースであるため)
- これはテストの構造を整える正しい方法ですか?私は[BeforeScenario]/[AfterScenario]などで使うことができるSpecFlowフックについて読んだことがありますが、これらが背景タグよりも考慮されるべきかどうかはわかりません。
- 避けることができる重複がたくさんあるような気がします。たとえば、有効なテストケースを別のテストのステップとして参照するだけで、テストケースとステップを作成するのではなく、 SpecFlowはどのように動作するはずですか?
これまでのところ、このテストケースでは私の.featureファイルに次のようなことがあります。
Feature: RegisterUser
In order to register a user successfully
A valid username, email, password (and confirmation password) must be entered
@register
Scenario: Valid Register User
Given I have entered a username "testing", an email "[email protected]", a password "123456" and a confirmation password "123456"
When I press submit
Then the response code should be 200
And the user should be added to the database with verfied set to False
Background:
Given I have registered a user "testing", "[email protected]", "123456" and "123456"
@register
Scenario: Username Already Taken
Given I have entered a username "testing", an email "[email protected]", a password "123456" and a confirmation password "123456"
When I press submit
Then the response code should be 400
そして、私の手順ファイルのようなものです:
using NUnit.Framework;
using System;
using System.Net.Http;
using TechTalk.SpecFlow;
namespace Tests.Specs
{
[Binding]
public class RegisterUserSteps
{
private RegisterUserModel userInfo = new RegisterUserModel();
private RegisterUserController user = new RegisterUserController()
{
Request = new HttpRequestMessage(),
Configuration = new System.Web.Http.HttpConfiguration()
};
private ApiResponse response = new ApiResponse();
private static string userTableName = "usersTable";
private static string userTablePartitionKey = "USER_INFO";
private static string userTableRowKey = "[email protected]";
[Given(@"I have entered a username ""(.*)"", an email ""(.*)"", a password ""(.*)"" and a confirmation password ""(.*)""")]
public void GivenIHaveEnteredAUsernameAnEmailAPasswordAndAConfirmationPassword(string p0, string p1, string p2, string p3)
{
userInfo.Username = p0;
userInfo.Email = p1;
userInfo.Password = p2;
userInfo.ConfirmPassword = p3;
}
[When(@"I press submit")]
public void WhenIPressSubmit()
{
response = user.Register(userInfo);
}
[Then(@"the response code should be (.*)")]
public void ThenTheResponseCodeShouldBe(int p0)
{
Assert.AreEqual(p0, response.Status);
}
[Then(@"the user should be added to the database with verfied set to False")]
public void ThenTheUserShouldBeAddedToTheDatabaseWithVerfiedSetToFalse()
{
UserEntity user = AzureUtilities.RetrieveEntity<UserEntity>(userTableName, userTablePartitionKey, userTableRowKey);
Assert.IsNotNull(user);
Assert.AreEqual(userInfo.Username, user.Username);
Assert.AreEqual(userInfo.Email, user.RowKey);
Assert.IsFalse(user.Verified);
}
[Given(@"I have registered a user ""(.*)"", ""(.*)"", ""(.*)"" and ""(.*)""")]
public void GivenIHaveRegisteredAUserAnd(string p0, string p1, string p2, string p3)
{
//Using methods here that have been defined in previous steps
GivenIHaveEnteredAUsernameAnEmailAPasswordAndAConfirmationPassword(p0, p1, p2, p3);
WhenIPressSubmit();
ThenTheResponseCodeShouldBe(200);
}