Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

creating unit tests (semi-)automatically?

Is there a framework that supports generating some standard unit tests from annotations? An example of what I have in mind would be:

@HasPublicDefaultConstructor
public class Foo {

}

This would obviously be used to automatically generate a unit test that checks whether Foo has a default constructor. Am I the only person who thought of something like that yet? ;) While I'm most interested in Java, solutions in other languages would certainly be interesting, too.

EDIT: In response to S.Lott's answer, let me clarify:

I'm trying to test whether the class has a default constructor. (Of course that is just an example.) I could just do so by writing a test, but I find that quite tedious. So I'm looking for a tool that would process the annotations at compile time (via APT) and generate the test for me. Does something like that exist? If not, do you think it is a good idea?

like image 576
Kim Stebel Avatar asked Dec 28 '08 16:12

Kim Stebel


People also ask

How do you make unit test cases automatically?

To generate unit tests, your types must be public. Open your solution in Visual Studio and then open the class file that has methods you want to test. Right-click on a method and choose Run IntelliTest to generate unit tests for the code in your method. IntelliTest runs your code many times with different inputs.

Can unit testing be automated?

Unit testing can be done manually but is usually automated. Unit testing is a part of the test-driven development (TDD) methodology that requires developers to first write failing unit tests. Then they write code in order to change the application until the test passes.

How do I create a junit test case automatically in Intellij?

Right-click the test root folder or package in the test root folder in which you want to create a new test and select New | Java Class. Name the new class and press Enter . Press Alt+Insert and select Test Method to generate a new test method for this class. Name the new method and press Enter .


2 Answers

The question is whether "polluting" the production code with Unit Testing information in the form of additional annotations is really such a good idea. Personally I'd vote against it.

like image 154
cdecker Avatar answered Sep 17 '22 22:09

cdecker


It sounds like you are looking for a programming language with more declarative expressive power than standard Java. The tests you postulate fill in the gaps until the compiler can check the semantics of the declarations.

I don't know of any tool that converts from the kind of annotations you suggest into automated tests. It sounds like a nice TDD exercise, though, especially with the recent(-ish) compiler APIs.

like image 43
Kent Beck Avatar answered Sep 20 '22 22:09

Kent Beck