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?
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.
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.
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 .
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With