简体   繁体   English

单元测试多个断言

[英]Unit testing multiple asserts

I am trying to unit test a method in C# using MSTest. 我正在尝试使用MSTest对C#中的方法进行单元测试。 I am testing that the password complexity is forced by the ChangePasswordAsync method. 我正在测试ChangePasswordAsync方法强制密码复杂性。 My problem is I want to test this method with a range of parameters. 我的问题是我想用一系列参数测试这个方法。 Currently i have a unit test with multiple asserts to test the different parameters, is this a reasonable solution? 目前我有一个单元测试,有多个断言来测试不同的参数,这是一个合理的解决方案吗?

I know when i have used other unit testing frameworks there have been ways to test a method with different parameters. 我知道当我使用其他单元测试框架时,已经有方法来测试具有不同参数的方法。 Is there an attribute I can use upon the method to achieve this? 我可以在方法上使用一个属性来实现这个目的吗? My example unit test is below: 我的示例单元测试如下:

    /// <summary>
    /// Ensures that a password must meet the password complexity of a digit and an alphanumeric, 8 digits long and one
    /// special character.
    /// </summary>
    [TestMethod]
    public void TestPasswordComplexity()
    {
        var result = _UserManager.ChangePasswordAsync(_TestUser.Id, "Password123!", "1!").Result; //Changes the password.

        Assert.IsFalse(result.Succeeded);

        result = _UserManager.ChangePasswordAsync(_TestUser.Id, "Password123!", "123456789").Result; //Changes the password.

        Assert.IsFalse(result.Succeeded);

        result = _UserManager.ChangePasswordAsync(_TestUser.Id, "Password123!", "123456789!").Result; //Changes the password.

        Assert.IsFalse(result.Succeeded);

        result = _UserManager.ChangePasswordAsync(_TestUser.Id, "Password123!", "abcdefghijk").Result; //Changes the password.

        Assert.IsFalse(result.Succeeded);

        result = _UserManager.ChangePasswordAsync(_TestUser.Id, "Password123!", "abcdefghijK1!").Result; //Changes the password.

        Assert.IsTrue(result.Succeeded);
    }  

Or alternatively would you break each one into seperate unit tests? 或者你会把每个人分成单独的单元测试吗?

You can add data row annotations. 您可以添加数据行注释。

[TestMethod]
[DataRow ("1!")
[DataRow ("123456789")
[DataRow ("123456789!")
...
public void TestPasswordComplexity(string pass)
{
    var result = _UserManager.ChangePasswordAsync(_TestUser.Id, "Password123!", pass).Result; //Changes the password.

    Assert.IsFalse(result.Succeeded);
}

It will run the test method with each data row passed with the pass parameter. 它将使用pass参数传递每个数据行来运行测试方法。

Create a separate method for the fifth Assert.IsTrue(result.Succeeded) case 为第五个Assert.IsTrue(result.Succeeded)案例创建一个单独的方法

Write for every test a separate unit test method with a meaning full name. 为每个测试编写一个单独的单元测试方法,其含义为全名。 So when later on the test fails you find out easily what's the problem. 因此,当测试失败后,您很容易发现问题所在。

You have to distinguish between different tests and different test parameters. 您必须区分不同的测试和不同的测试参数。 Some of your asserts will fail because the password is for some reason to weak, some should pass. 你的一些断言会失败,因为密码由于某种原因变弱,有些应该通过。

For the case where you really need different parameters, there is the possibility to use DataSource (see http://msdn.microsoft.com/en-us/library/ms182527.aspx ) where you can add an external source as excel file for input. 对于您确实需要不同参数的情况,可以使用DataSource (请参阅http://msdn.microsoft.com/en-us/library/ms182527.aspx ),您可以在其中添加外部源作为excel文件输入。

Eg you could test passing passwords with the excel source. 例如,您可以使用excel源测试传递密码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM