0

我可以在代码 UI 的一个 .cs 文件中添加多个测试方法吗?

下面是我的代码。我有两个特点。1.登录和注销。

我创建了一个 CodedUITest1.cs 文件,我试图在其中添加多种方法。真的可以这样做吗

公共类 CodedUITest1 { 公共 CodedUITest1() { }

    [TestMethod]
    public void Login()
    {
        this.UIMap.Login();
        this.UIMap.Assert_Login();
        this.UIMap.LogOff();
    }

    [TestMethod]
    public void LogOff()
    {
       this.UIMap.LogOff();
    }
4

1 回答 1

0

是的,您可以在一个测试类中进行多个测试。您注意到的问题是什么?

通常,我使用 TestInitialize 属性为所有测试设置通用步骤,然后每个测试方法执行与该点不同的操作并执行断言等。

public class LoginPageTests
{
    BrowserWindow bw;
    [TestInitialize]
    public void GivenLoginPage()
    {
        bw = BrowserWindow.Launch("http://yoursite.com/loginPage");
    }

    [TestMethod]
    public void WhenSupplyingValidCredentials_ThenLoginSucceedsAndAccountsPageIsShown()
    {
        Assert.IsTrue(bw.Titles.Any(x => "Login"));
        HtmlEdit userNameEdit = new HtmlEdit(bw);
        userNameEdit.SearchProperties.Add("id", "userName");
        userNameEdit.Text = "MyUserName";

        HtmlEdit passEdit = new HtmlEdit(bw);
        passEdit.SearchProperties.Add("id", "pass");
        passEdit.Text = "MyPassword";

        HtmlButton loginButton = new HtmlButton(bw);
        Mouse.Click(loginButton);

        // probably can one of the WaitFor* methods to wait for the page to load
        Assert.IsTrue(bw.Titles.Any(x => "Accounts"));
    }

    [TestMethod]
    public void WhenNoPassword_ThenButtonIsDisabled()
    {
        HtmlEdit userNameEdit = new HtmlEdit(bw);
        userNameEdit.SearchProperties.Add("id", "userName");
        userNameEdit.Text = "MyUserName";

        HtmlButton loginButton = new HtmlButton(bw);
        Assert.IsFalse(loginButton.Enabled);
    }
}
于 2016-05-16T20:01:30.380 回答