Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to decide test cases for unit tests?

Tags:

unit-testing

I'm just getting into unit testing, and have written some short tests to check if function called isPrime() works correctly.

I've got a test that checks that the function works, and have some test data in the form of some numbers and the expected return value.

How many should I test? How do I decide on which to test? What's the best-practices here?

One approach would be to generate 1000 primes, then loop through them all, another would be to just select 4 or 5 and test them. What's the correct thing to do?

like image 899
Rich Bradshaw Avatar asked Nov 20 '08 19:11

Rich Bradshaw


People also ask

What are three test cases you should go through in unit testing?

A unit test typically features three different phases: Arrange, Act, and Assert (sometimes referred to as AAA). For a unit test to be successful, the resulting behavior in all three phases must be in line with expectations.

How do you decide how many test cases?

There is no exact formula for writing test cases. But in my exp I found that ,Total test cases = number of inputs * 1.6Just u apply to one requirement &check it.

What is unit test cases example?

A Real-world ExampleThe above unit test “asserts” that 5 + 10 is equal to 15. If the Add function returns anything else Assert. IsEqual result in error and the test case will fail. After you write your test cases, you will run them to verify that everything is working correctly.


2 Answers

I've also been informed that every time a bug is found, you should write a test to verify that it is fixed. It seems reasonable to me, anyway.

like image 176
Lieutenant Frost Avatar answered Sep 24 '22 18:09

Lieutenant Frost


You'd want to check edge cases. How big a prime number is your method supposed to be able to handle? This will depend on what representation (type) you used. If you're only interested in small (really relative term when used in number theory) primes, you're probably using int or long. Test a handful of the biggest primes you can in the representation you've chosen. Make sure you check some non-prime numbers too. (These are much easier to verify independently.)

Naturally, you'll also want to test a few small numbers (primes and non-primes) and a few in the middle of the range. A handful of each should be plenty. Also make sure you throw an exception (or return an error code, whichever is your preference) for numbers that are out of range of your valid inputs.

like image 40
Bill the Lizard Avatar answered Sep 23 '22 18:09

Bill the Lizard