|
对于自动化测试的断言的重要性,上次在Silk4Jul里已经简单介绍了,在这里也不多说了,总之自动化测试必须有结果验证,否则谁都不知道自动化最终会跑成什么样,跑到哪里去了!
那今天我们再一起来看一下Silk4NET是怎么断言的。
断言前的全部代码如下:
using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SilkTest.Ntf;
using SilkTest.Ntf.XBrowser;
namespace Silk4NETProject1
{
[SilkTestClass]
public class UnitTest1
{
private readonly Desktop _desktop = Agent.Desktop;
[TestInitialize]
public void Initialize()
{
BrowserBaseState baseState = new BrowserBaseState();
baseState.Execute();
}
[TestMethod]
public void TestMethod1()
{
BrowserApplication webBrowser = _desktop.BrowserApplication();
BrowserWindow browserWindow = webBrowser.BrowserWindow();
browserWindow.DomTextField("//INPUT[@id='kw']").Click(MouseButton.Left, new Point(225, 19));
browserWindow.DomTextField("//INPUT[@id='kw']").TypeKeys("51testing");
webBrowser.DomButton("//INPUT[@id='su']").Click(MouseButton.Left, new Point(48, 18));
}
}
}
增加断言后的完整代码如下:
using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SilkTest.Ntf;
using SilkTest.Ntf.XBrowser;
namespace Silk4NETProject1
{
[SilkTestClass]
public class UnitTest1
{
private readonly Desktop _desktop = Agent.Desktop;
[TestInitialize]
public void Initialize()
{
BrowserBaseState baseState = new BrowserBaseState();
baseState.Execute();
}
[TestMethod]
public void TestMethod1()
{
BrowserApplication webBrowser = _desktop.BrowserApplication();
BrowserWindow browserWindow = webBrowser.BrowserWindow();
browserWindow.DomTextField("//INPUT[@id='kw']").Click(MouseButton.Left, new Point(225, 19));
browserWindow.DomTextField("//INPUT[@id='kw']").TypeKeys("51testing");
webBrowser.DomButton("//INPUT[@id='su']").Click(MouseButton.Left, new Point(48, 18));
String getTextValue;
getTextValue = browserWindow.DomTextField("//INPUT[@id='kw']").GetProperty("value").ToString();
System.Console.WriteLine(getTextValue);
Assert.AreEqual("51testing1", getTextValue);
}
}
}
Assert在断言正确的情况下,看不出任何的运行信息,只有当断言失败时,才会显示运行后的值的不一致之处,如下代码是先让断言失败:
Assert.AreEqual("51testing1", getTextValue);
看运行后的结果如何。如下图所示:
|
|