5

我是 BDD、Specflow 和 WatiN 的新手。我想使用这些工具来自动化我的 ASP.NET MVC 应用程序的验收测试。

我已经弄清楚了如何基本使用这些工具,并且我成功构建了我的第一个验收测试:登录网站。

这是这个测试的小黄瓜:

Feature: Log on to the web 
    As a normal user
    I want to log on to the web site

Scenario: Log on
    Given I am not logged in
    And I have entered my name in the username textbox
    And I have entered my password in the password textbox
    When I click on the login button
    Then I should be logged and redirected to home

现在,我想写一堆其他的测试,它们都需要对用户进行身份验证。例如:

Feature: List the products 
    As an authenticated user
    I want to list all the products

Scenario: Get Products
    Given I am authenticated
    And I am on the products page
    When I click the GetProducts button
    Then I should get a list of products

让我烦恼的是,为了使这个测试独立于其他测试,我必须编写代码才能再次登录网站。这是要走的路吗?我怀疑。

我想知道是否有最佳实践可以用于测试这样的场景。我应该保持浏览器打开并让测试在同一个浏览器上按特定顺序运行吗?还是应该将 MVC 应用程序置于特定状态?

4

2 回答 2

4

为此,我们有一个特定的 Given 步骤,在 Gherkin 中看起来像这样:

Given I am signed in as user@domain.tld

就像您提到的,此步骤基本上重复使用其他步骤来登录用户。我们还有一个需要密码的“重载”,以防测试用户有非默认测试密码:

Given I am signed in as user@domain.tld using password "<Password>"

[Binding]
public class SignInSteps
{
    [Given(@"I am signed in as (.*)")]
    public void SignIn(string email)
    {
        SignInWithSpecialPassword(email, "asdfasdf");
    }

    [Given(@"I am signed in as (.*) using password ""(.*)""")]
    public void SignInWithSpecialPassword(string email, string password)
    {
        var nav = new NavigationSteps();
        var button = new ButtonSteps();
        var text = new TextFieldSteps();
        var link = new LinkSteps();

        nav.GoToPage(SignOutPage.TitleText);
        nav.GoToPage(SignInPage.TitleText);
        nav.SeePage(SignInPage.TitleText);
        text.TypeIntoTextField(email, SignInPage.EmailAddressLabel);
        text.TypeIntoTextField(password, SignInPage.PasswordLabel);
        button.ClickLabeledSubmitButton(SignInPage.SubmitButtonLabel);
        nav.SeePage(MyHomePage.TitleText);
        link.SeeLinkWithText("Sign Out");
    }
}

我认为这是最好的方法,因为您不能保证所有测试都以特定顺序运行。

不过,您也可以使用 SpecFlow 标签执行此操作,并让该标签执行 BeforeScenario。这可能看起来像这样:

Feature: List the products 
    As an authenticated user
    I want to list all the products

@GivenIAmAuthenticated
Scenario: Get Products
    Given I am on the products page
    When I click the GetProducts button
    Then I should get a list of products

[BeforeScenario("GivenIAmAuthenticated")]
public void AuthenticateUser()
{
    // code to sign on the user using Watin, or by reusing step methods
}

...我应该将 MVC 应用程序置于特定状态吗?

在用户登录时,需要将其置于特定状态的不是 MVC 应用程序,而是需要置于特定状态的浏览器——即写入身份验证 cookie。我不确定你是否可以这样做,因为 auth cookie 是加密的。我总是发现让 SF 在每个场景开始时通过身份验证步骤更容易。

于 2012-06-01T18:36:14.217 回答
1

您可能需要考虑进一步完善您的解决方案。阅读Dan North 的 Who's Domain 是什么,然后问问自己这两个场景是否真的属于一起。我发现 danludwig 建议的方法是有价值的,但是根据他们的领域分离你的场景更有价值。把它想象成重构你的场景。

You can still build one scenario from the activities of another, but ask yourself, do you really need to go through the same steps, or would providing a mocked "Already logged in" object to your session work better.

于 2012-06-20T15:59:28.603 回答