Check several different example inputs in a single test?

前端 未结 7 941
感情败类
感情败类 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:26

    BDD and/or Context/Specification frameworks like SubSpec which happens to have some salient examples manage this by treating each batch of related assertions as a separate Observation block that one gives a name or descriptive label to.

    The key elements of a good test that this pushes one towards are:

    1. you have AAA / GWT separation of each of the phases - you're forced to really think about which is which [and will often quickly realise when things need to be broken up more]
    2. You end up with good names/descriptions on Observations (Row Tests generally leave you with [less maintainable] comments at best)
    3. When tests 10 and 17 out of 24 [as with Row Tests] you have the status of each of the tests to assist you in narrowing down the solution

    Row Tests can be appropriate in some cases when you really are doing a simple table or matrix based suite of things.

    Also xUnit.net's PropertyData can be powerful and appropriate in some cases as a way of doing some of the tricks in the other anwers.

提交回复
热议问题