Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to programmatically generate JUnit test cases and suites?

I have to write a very large test suite for a complex set of business rules that are currently captured in several tabular forms (e.g., if parameters X Y Z are such and such, the value should be between V1 and V2). Each rule has a name and its own semantics.

My end goal is to have a test suite, organized into sub test suites, with a test case to each rule.

One option is to actually hard code all these rules as tests. That is ugly, time consuming, and inflexible.

Another is to write a Python script that would read the rule files and generate Java classes with the unit tests. I'd rather avoid this if I can. Another variation would be to use Jython.

Ideally, however I would like to have a test suite that would read the files, and would then define sub-suites and tests within them. Each of these tests might be initialized with certain values taken from the table files, run fixed entry points in our system, and then call some validator function on the results based on the expected value.

Is there a reasonable way to pull this off using only Java?

Update: I may have somewhat simplified our kind of rules. Some of them are indeed tabular (excel style), others are more fuzzy. The general question though remains as I'm probably not the first person to have this problem.

like image 276
Uri Avatar asked Oct 29 '09 21:10

Uri


People also ask

How do I create a test case and test suite in JUnit?

Create Test Suite ClassAttach @RunWith(Suite. class) Annotation with the class. Add reference to JUnit test classes using @Suite. SuiteClasses annotation.

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 we automate JUnit?

JUnit is best at creating repeatable test cases. If there is a need to run a test in JUnit 100 times, one doesn't need to write it 100 times or not even execute it manually 100 times. This process is automated. Automation testing is usually preferred over manual testing because of faster and reliable results.

How do I run JUnit tests automatically?

Running Single Test Class. To run JUnit 5 tests from Java code, we'll set up an instance of LauncherDiscoveryRequest. It uses a builder class where we must set package selectors and testing class name filters, to get all test classes that we want to run.


2 Answers

Within JUnit 4 you will want to look at the Parameterized runner. It was created for the purpose you describe (data driven tests). It won't organize them into suites however.

In Junit 3 you can create TestSuites and Tests programatically. The answer is in Junit Recipes, which I can expand if you need it (remember that JUnit 4 can run Junit 3 tests).

like image 129
Kathy Van Stone Avatar answered Sep 27 '22 20:09

Kathy Van Stone


Have you considered using FIT for that?

You seem to have the tables already ready, and "business rules" sounds like "business people write them using excel".

FIT is a system for checking tests based on tables with input->expected output mappings, and a open source java library for running those tests is available.

like image 29
ankon Avatar answered Sep 27 '22 20:09

ankon