Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatic generation of Unit test cases for .NET and Java [closed]

Is there a good tool to generate unit test cases given say a .NET or Java project, it generates unit test cases that would cover an almost 100% code coverage. The number of test cases could be directly proportional to the cyclomatic complexity of the code (the higher the nesting of loops and conditions the higher the cyclomatic complexity) where the higher the cyclomatic complexity, the greater the set of test cases are generated. I'm not expecting it to be fully functional (say I'm going to build the unit tests and run it after its been generated), but I would say that it can have a template style in the test case where you are to modify the case that suits your intended needs. But it should also have a proper setup and teardown method and is good enough to detect if mock objects for unit testing should be used should there be any dependencies. So, is there such a tool that exists?

like image 602
yoitsfrancis Avatar asked Mar 27 '09 09:03

yoitsfrancis


People also ask

Can unit test cases be automated?

Unit testing is a software development and testing approach in which the smallest testable parts of an application, called units, are individually and independently tested to see if they are operating properly. Unit testing can be done manually but is usually automated.

How do you auto generate JUnit test cases?

Open Eclipse, and choose the project you want to create test cases for. In the Package Explorer, select the java class you want to generate the Junit test for. Go to File -> New -> Junit Test Cases.


4 Answers

For Java, you can check EvoSuite, which is open source and currently active (disclaimer, I am one of its contributors). Also see related question for a list of more tools.

like image 167
arcuri82 Avatar answered Sep 28 '22 01:09

arcuri82


For .NET, Microsoft has Pex which will hopefully go mainstream for .NET 4.0, along with Code Contracts. I highly recommend watching the Channel 9 video.

It strikes me that this sort of thing is very good for very data-driven classes - parsers etc. I can't see that I'd very often start off with it, but a useful tool to have in your armoury nonetheless.

like image 39
Jon Skeet Avatar answered Sep 27 '22 01:09

Jon Skeet


For C# (or .NET in general), PEX might be that tool. It works at the IL level, and attempts to force its way into every branch. It has successfully uncovered a wide range of bugs (in the BCL etc).

like image 44
Marc Gravell Avatar answered Sep 25 '22 01:09

Marc Gravell


Although it seems counter-intuituve, you may also be interested in random test generation frameworks. Research has proven that it can be just as effective in finding bugs than systematic approaches based on coverage, as you suggest.

Check out Randoop both for .NET and Java. It works by generating a more or less random sequence of method calls, and checks contracts, crashes etc. It is fully automatic.

Also you may want to check out some other random testing tools based on QuickCheck, e.g. for Java, Scala, F#. that are more similar to Pex, i.e. you give a specification, or parametrized unit test, and the tool checks it for a number of generated input arguments.

I've found that this "parametrized" way of writing unit tests is actually a lot more natural in at least 60% of the cases, and finds lots more bugs.

like image 32
Kurt Schelfthout Avatar answered Sep 27 '22 01:09

Kurt Schelfthout