WatiN:如何使用自动化测试运行结果,因为我无法获得报告测试运行结果的方法

本文关键字:测试运行 结果 方法 报告 因为 何使用 自动化 WatiN | 更新日期: 2023-09-27 18:10:51

我已经用WATIN自动化了一个模块,现在我希望测试结果显示为通过/失败状态等(报告),WATIN中是否有任何功能可以执行我所需的操作。

比如我有一个像

这样的代码
    public static void TestSelectPatientLink()
    {
        try
        {
            Link lnkSelectPatientb = ie.Link(Find.ByTitle("Search patient"));
            lnkSelectPatientb.Blur();
            lnkSelectPatientb.ClickNoWait();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
    }

如何获得报告,当这段代码在VS 2010中运行时发生了什么,它是失败还是通过,如果失败,错误等,如何报告这些事情

**仅供参考,我正在使用Nunit与WatiN

WatiN:如何使用自动化测试运行结果,因为我无法获得报告测试运行结果的方法

不,没有任何内置到WatiN的功能来支持这个,但是GarethStephenson的帖子是正确的。你可以写一个NUnit测试,它会给你一个通过/失败。

首先,要让IE使用NUnit,你需要在app.config 中添加以下内容
<configSections>
  <sectionGroup name="NUnit">
    <section name="TestRunner" type="System.Configuration.NameValueSectionHandler"/>
  </sectionGroup>
</configSections>
<NUnit>
  <TestRunner>
    <!-- Valid values are STA,MTA. Others ignored. -->
    <add key="ApartmentState" value="STA" />
  </TestRunner>
</NUnit>

下面是一个示例测试。它加载google主页,抓取一些元素并断言它们存在:-

using WatiN.Core;
using NUnit.Framework;
namespace ConsoleApplication1
{
    [TestFixture]
    public class AutomatedTests
    {
        [Test]
        public void DoGoogleTest()
        {
            using (IE browser = new IE())
            {
                browser.GoTo("www.google.co.uk");
                Div logoDiv = browser.Div("hplogo");
                Assert.IsTrue(logoDiv.Exists, "Logo div does not exist");
                TextField searchText = browser.TextField("lst-ib");
                Assert.IsTrue(searchText.Exists, "Search text field does not exist");
                Button searchBtn = browser.Button(Find.ByName("btnK"));
                Assert.IsTrue(searchBtn.Exists, "Search button does not exist");
                Button nonExistantButton = browser.Button("garbagegarbagegarbage");
                // This will cause the test to fail because the link doesn't (shouldn't!) exist.
                // Comment it out and the test should pass
                Assert.IsTrue(nonExistantButton.Exists, "Non-existant button does not exist");
            }
        }
    }
}

不幸的是,NUnit不能自动与Visual studio测试视图/测试列表窗口集成。你的选项是:-

  • 安装NUnit
  • 安装Gallio(这将集成并有利于报告)
  • 在外部运行测试(可以通过VS完成)

上面的代码给出了结果:-

ConsoleApplication1.AutomatedTests.DoGoogleTest:
  Non-existant button does not exist
  Expected: True
  But was:  False

如果注释掉最后一行,则不会报告错误。

如果你需要更多的信息,请告诉我。HTH !

EDIT为VS2010添加了Visual NUnit扩展链接

你总是可以把那个方法转换成一个单元测试方法,使用NUnit或类似的。

[Test]
public void TestSelectPatientLink()
{
    try
    {
        Link lnkSelectPatientb = ie.Link(Find.ByTitle("Search patient"));
        lnkSelectPatientb.Blur();
        lnkSelectPatientb.ClickNoWait();
    }
    catch (Exception ex)
    {
        // Capture the error screen so you can see what went wrong
        ie.CaptureWebPageToFile("Error.jpg");
        // Fail the test, use the unit testing framework's reporting to get your pass/fail
        Assert.Fail(ex.ToString());
    }
}

订阅非单元事件(testFinished, testStarted)可能会有所帮助。但它需要为nunit开发插件Br/Vitalii