Automatical Test in BDD

Behavior-driven development(行为驱动开发)这里我们简为BDD。这篇主要以Google搜索测试的例子来说明从零搭建出一个自动化测试的框架。本测试案例来 源于WATIN官网的Hello World,我们来把这个测试转化于行为驱动开发模式下的测试。当然这个例子远远谈不上框架,只是一个入门的介绍。

首先我们来看一下WATIN的测试:http://watin.org/

[Test]
public void SearchForWatiNOnGoogle()
{
using (var browser = new IE("http://www.google.com"))
{
browser.TextField(Find.ByName("q")).TypeText("WatiN");
browser.Button(Find.ByName("btnG")).Click();
 
Assert.IsTrue(browser.ContainsText("WatiN"));
}
}
 

安装SpecFlow

http://www.specflow.org/downloads/installer.aspx

本文安装的版本是1.9,它可以和Visual Studio 2012集成一起。安装好之后,启动VS, 新建项目或打开已有项目,添加新的文件,可以看到有…Feature, Steps…等文件。

1w4v3ks4.hh3[7]

之后我们就开始搭建BDD模式下的测试。VS New Project > Add one Class Library with project name GoogleTestDemo

加载需要的工具或程序集

加载WatiN.Core.dll, TechTalk.SpecFlow.dll, nunit.framework.dll

配置App.Config

在App.Config中我们需要设置TestRunner和ApartmentState

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="specFlow" type="TechTalk.SpecFlow.Configuration.ConfigurationSectionHandler, TechTalk.SpecFlow"/>
    <sectionGroup name="NUnit">
      <section name="TestRunner" type="System.Configuration.NameValueSectionHandler"/>
    </sectionGroup>
  </configSections>
  <appSettings>
  </appSettings>
  <specFlow>
    <!-- For additional details on SpecFlow configuration options see https://github.com/techtalk/SpecFlow/wiki/Configuration -->
  </specFlow>
  <NUnit>
    <TestRunner>
      <!-- Valid values are STA,MTA. Others ignored. -->
      <add key="ApartmentState" value="STA"/>
    </TestRunner>
  </NUnit>
  <startup>
  <supportedRuntime version="v2.0.50727"/></startup>
</configuration>

SpecFlow默认的TestRunner是NUnit,如果需要使用MsTest或其他工具,需要App.Config中设置。这里需要设置ApartmentState为STA

添加Search.feature文件,写验收测试的Feature和Scenarios

Feature: Search

In order to find related information through network

I want to use Google search to find out information

Scenario: Search WatiN

Given I am on Google search page

When I search WatiN

Then I should be able to find WatiN

添加SpecFlow Step Definition文件

SearchSteps.cs, 删除类中的默认方法。我们可以回到Search.feature文件中,鼠标选中Given步骤,按F12,SpecFlow会帮我们默认生成一个方法来 绑定Given步骤,选择Yes,就可以把这个方法粘帖到SearchSteps.cs中去

[Given(@"I am on Google search page")]

public void GivenIAmOnGoogleSearchPage()

SpecFlow通过.NET的特性来把描述性的测试语言(Gherkin)Given I am on Google search page和C#的方法public void GivenIAmOnGoogleSearchPage()绑定起来。这也是BDD框架最核心的地方,BDD使开发和测试以及其他非技术人员更好地沟通和 合作,实现文档即测试。

创建WebBrowser的静态类

public class WebBrowser
{
    private const string BrowserKey = "browser";
    private static IE _ieInstance;

    public static IE Current
    {
        get
        {
            if (!FeatureContext.Current.ContainsKey(BrowserKey))
                FeatureContext.Current[BrowserKey] = _ieInstance;

            return (IE)FeatureContext.Current[BrowserKey];
        }
    }

    [BeforeFeature]
    public static void StartBrowser()
    {
        _ieInstance = new IE();
        _ieInstance.ClearCache();
        _ieInstance.ClearCookies();
    }

    [AfterFeature]
    public static void CloseAnyUsedBrowsers()
    {
        _ieInstance.Dispose();
        _ieInstance.Close();
    }
}

这里的WebBrowser使用Singleton的模式,能够更好实现在不同的步骤传递相同的浏览器对象,并且很好的屏蔽运行过程中其他浏览器的 影响。在上面的例子中我们还加入了 [BeforeFeature]和[AfterFeature]的方法,这十分类似我们应用NUnit框架中的[BeforeFixture]和 [TearDown]特性。他们用来实现每个测试之前初始化和完成后的清除工作。当然你也可以根据项目的情况加入更多的方法。

WATIN实现测试方法

类似Given,生成When,Then步骤对应的方法,后面需要在每一个方法中使用WATIN来写测试代码,比较快捷的是把我们刚才WATIN官网的例子Copy到Steps中相应的方法。

[Binding]
public class SearchSteps
{
    [Given(@"I am on Google search page")]
    public void GivenIAmOnGoogleSearchPage()
    {
        WebBrowser.Current.GoTo("www.google.com");
    }

    [When(@"I search WatiN")]
    public void WhenISearchWaiN()
    {
        WebBrowser.Current.TextField(Find.ById("gbqfq")).TypeText("WatiN");
        WebBrowser.Current.Button(Find.ById("gbqfb")).Click();
    }

    [Then(@"I should be able to find WatiN")]
    public void ThenIShouldBeAbleToFindWatiN()
    {
        Thread.Sleep(2000);
        Assert.IsTrue(WebBrowser.Current.Link(Find.ByUrl("http://watin.org")).Exists);
    }
}

编译成功后使用NUnit来运行测试

qxzwoopc.d05

原文地址:https://www.cnblogs.com/m4LWJ/p/2912830.html