Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check several different example inputs in a single test?

Tags:

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.

However, I can come up with a bunch of different ways to test the same function ([email protected]; [email protected]; test.test.com, etc).

Do I put all the incantations that I need to check in the same, single test with several ASSERTS or do I write a new test for every single thing I can think of?

Thanks!

like image 364
Chris Burgess Avatar asked May 06 '09 19:05

Chris Burgess


People also ask

Which annotation makes it run a test multiple times with different parameters?

JUnit 5, the next generation of JUnit, facilitates writing developer tests with shiny new features. One such feature is parameterized tests. This feature enables us to execute a single test method multiple times with different parameters.

Can you test all possible combinations of user input?

If the number of possible combinations is reasonably small, you can test all of them. But if it is not the case and it is very difficult to maintain tests that cover all possible inputs, I would recommend testing only the most typical cases and edge cases(if there are any).

What does .test do in Java?

Java testing provides thorough and functioning test cases that can test every aspect of your application. A JUnit test case is exactly what it sounds like: a test scenario measuring functionality across a set of actions or conditions to verify the expected result. JUnit is a simple framework to write repeatable tests.


2 Answers

Most testing frameworks now support some sort of data based testing to let you run the same test on multiple data sets.

See the ValuesAttribute in NUnit.

xUnit.net, MBUnit and others have similar methods.

like image 80
Paul Alexander Avatar answered Oct 05 '22 06:10

Paul Alexander


Generally I create many different tests, and give each it's own name. For example let's say that there are 3 different regexes {A, B, & C} for matching an email address. The function checks the incoming email for a match and accepts the first match found.

I would have the following tests.

  • ATypeEmailShouldMatchPatternA
  • BTypeEmailShouldMatchPatternB
  • CTypeEmailShouldMatchPatternC
  • BadEmailShouldNotMatchAnyPattern
  • EmailCompatibleWithPatternAAndPatternBShouldBeMatchedByA
  • EmailCompatibleWithPatternAAndCShouldBeMatchedByA
  • EmailCompatibleWithPatternBAndCShouldBeMatchedByB

Generally I'd put one assert in each test.

Sometimes, however, I'll put more than one assert in a test if the asserts are all checking different parts of the same thing. For example, let's say I have a function that finds all the emails matching pattern A. My test feeds in a list of emails but only one matches pattern A. The function returns a list, and I expect that list to have only one element in it. So I would assert two things:

  1. That the list has one element in it.
  2. That the one element is the email that matches pattern A.
like image 25
Uncle Bob Avatar answered Oct 05 '22 05:10

Uncle Bob