Check several different example inputs in a single test?

前端 未结 7 858
感情败类
感情败类 2021-01-17 07:31

Let\'s say I want to write a function that validates an email address with a regex. I write a little test to check my function and write the actual function. Make it pass.

相关标签:
7条回答
  • 2021-01-17 08:31

    As @Paul mentioned several test frameworks support RowTests. Using that feature you can write something as monstrous as this:

    [TestCase ("test@test.com", true)]
    [TestCase ("x!x@test.com", true)]
    [TestCase ("x#x@test.com", true)]
    [TestCase ("x$x@test.com", true)]
    [TestCase ("x%x@test.com", true)]
    [TestCase ("x&x@test.com", true)]
    [TestCase ("x'x@test.com", true)]
    [TestCase ("x*x@test.com", true)]
    [TestCase ("x+x@test.com", true)]
    [TestCase ("x-x@test.com", true)]
    [TestCase ("x/x@test.com", true)]
    [TestCase ("x=x@test.com", true)]
    [TestCase ("x?x@test.com", true)]
    [TestCase ("x^x@test.com", true)]
    [TestCase ("x_x@test.com", true)]
    [TestCase ("x`x@test.com", true)]
    [TestCase ("x{x@test.com", true)]
    [TestCase ("x{x@test.com", true)]
    [TestCase ("x|x@test.com", true)]
    [TestCase ("x}x@test.com", true)]
    [TestCase ("x~x@test.com", true)]
    [TestCase ("test", false)]
    [TestCase ("", false)]
    [TestCase (null, false)]
    public void IsEmail_Should_Match_Valid_Email_Addresses(string target, bool result)
    {
        Assert.AreEqual(result, target.IsEmail());
    }
    

    Or you could do the same with a bunch of asserts. It's common to assert multiple properties on an object after performing some action. I think the above solution is more readable though.

    0 讨论(0)
提交回复
热议问题