I have my junit test classes.I am running these class with ant.I am using batchtest attribute with parameter includes="*/ Test *.class" but i have classes with name like * test * that includes no test method and when i run it gives error "no tests found".Is there a option like if no test in the class then skip?
To answer the question you need to supply a better pattern.
Potential solution:
Refactor all legitimate test classes to BEGIN with the prefix Test and than change the ant script to this:
includes="*/Test*.class"
Why do you have classes with the name "Test" in them that aren't tests? I highly recommend following a naming convention that puts Test at the beginning and removing test from your file names that are in fact not tests.
Related
I have 2 classes under src/test/java that I want to run: scen1.class and scen2.class. Both of these have #Test annotated methods.
I have another class called JunitDefinitions.class that has only #Before, #After, #BeforeClass and #AfterClass methods (no #Test).
This is also under src/test/java but under a different package.
Assuming I have the default pom.xml, what should I add to it in order to be able to execute all these 3 classes?
Rename your test classes to Scen1Test.java and Scen2Test.java or include JunitFW.java, Scen1.java and Scen2.java using <includes> configuration, as it's described in the documentation.
The class JunitFW only contains a #Before, so it's not detected as a Test. You have to move the #Before to a class containing #Test or include a #Test in JunitFW class and rename it to JunitFWTest to make it work.
By default, the Surefire Plugin will automatically include all test classes with the following wildcard patterns:
"**/Test*.java" - includes all of its subdirectories and all Java filenames that start with "Test".
"**/*Test.java" - includes all of its subdirectories and all Java filenames that end with "Test".
"**/*Tests.java" - includes all of its subdirectories and all Java filenames that end with "Tests".
"**/*TestCase.java" - includes all of its subdirectories and all Java filenames that end with "TestCase".
Apart from the renaming of your classes as something like Scen1Test.java and Scen2Test.java as suggested by #viniciusartur, which shall help Maven to recognize the test classes to execute them using surefire-plugin.
Another point to note here is that the reason due to which the #Before, #BeforeClass, #After etc are not executed independently without a #Test method is that only
The Test annotation tells JUnit that the public void method to which
it is attached can be run as a test case. To run the method, JUnit
first constructs a fresh instance of the class then invokes the
annotated method.
From the documentation of #Before in JUnit
Annotating a public void method with #Before causes that method to be
run before the Test method. The #Before methods of superclasses will
be run before those of the current class.
So inferring as this, while annotations are processed, if there is no #Test annotation present in the class under /src/test/java(relative to the question based on maven), no further annotations are meaningful to be processed.
Just to note, if you extend this class with another SubClassTest.java consisting of a #Test method, all these methods would be executed then. Since they are processed based on what(#Test) to act on to.
The solution I am using to create a JUnit test suite dynamically can be found in this similar question here: How do I Dynamically create a Test Suite in JUnit 4?
The solution I am trying to adapt looks like such:
#RunWith(AllTests.class)
public class SomeTests
{
public static TestSuite suite()
{
TestSuite suite = new TestSuite();
suite.addTest(new JUnit4TestAdapter(Test1.class));
suite.addTest(new JUnit4TestAdapter(Test2.class));
return suite;
}
}
However, I would not only like to be able to dynamically create a test suite, but also be able to allow for the user running my program to specify which tests they would like to run using a properties file.
Is there a way I can annotate my classes with a String such that I can get the actual class type given the annotation String? Are there any viable solutions for this or is it just bad practice in general?
As I understand you would like to mark your test classes as belonging to one or more groups, then user defines the group of test cases to execute. Is it correct?
Bad or good this practice is, it is already implemented in TestNG. There is no such feature in JUnit. But you could easily add annotation scanner to your code and select proper test classes dynamically (e.g. by using https://github.com/ronmamo/reflections to collect all your annotated tests in class path with just single line of code).
I am having a excel sheet having testcases name and some values related to it
eg: TestCase2 Value2, TestCase1 Value1.
I have a Junit class having test methods(TestCase1(), TestCase2()) as the same name I have in excel sheet
So when I run my Junit class I want the test methods to execute serially the way they are mentioned in excel file as in this case testcase2 should execute before testcase1.
Method 1:
Use Excel to construct and launch command lines, with Excel/windows capabilities (VBscript, ...).
To launch junit tests:
java org.junit.runner.JUnitCore [test class name]
see that:
How to run JUnit test cases from the command line
Note that you can launch a class, not a particular method.
For particular method, see that: Run single test from a JUnit class using command-line
For the order of methods, see below.
But, how you get and exploit results is another thing.
Method 2 (prefered): Control everything from Java:
Read excel file, with proper library:
How to read and write excel file in java
or, change it to csv, or OpenOffice (odf), and read with other libraries.
Iterate, and launch each method by its name by reflexivity:
getMethod and Invoke.
See that: How do I invoke a Java method when given the method name as a string?
Order of tests:
Set this before the class:
#FixMethodOrder(MethodSorters.NAME_ASCENDING)
Then your tests will be executed in lexical order
Hope it helps !
I have a class Something and a JUnit test class TestSomething.
How can I add the test method names of TestSomething to the documentation of Something?
you can run javadoc for test classes too: see tes-javadoc goal http://maven.apache.org/plugins/maven-javadoc-plugin/test-javadoc-mojo.html
Using JUnit 4, my ant build script that invokes junit with the task always reports the test class names as "unknown". Is there a way of fixing this that does not involve deriving from TestCase?
I declare my test methods with the #Test annotation, e.g.:
SomeTest.java:
public class SomeTest {
#Test
public void doSomething() { .. }
}
and collect them together in a TestSuite:
AllTests.java:
#RunWith(Suite.class)
#Suite.SuiteClasses( {
SomeTest.class
})
public class AllTests {
}
I then invoke the ant task via NetBeans, which has nice integrated support for printing results of a JUnit run. Everything seems find except the test names are always "unknown", e.g.:
6 tests passed, 2 tests failed, 2 tests caused an error (8.4s)
com.mystuff.AllTests FAILED
Unknown passed (0.0s)
Unknown passed (0.0s)
Unknown passed (0.0s)
Unknown passed (0.0s)
Unknown passed (0.0s)
Unknown passed (0.0s)
Unknown FAILED (0.0s)
Unknown caused an ERROR: at org.hibernate.somethingorother.java:1234 (0.0s)
..etc..
The tests operate correctly, and I can usually figure out the point of failure from the stack trace that's logged, but everything being reported as 'Unknown' is simply annoying and obtuse.
Having read up on a few SO posts, I discovered there are two ways to set your tests up, the old JUnit 3 way of deriving from TestCase, and the new Junit 4 way of using #Test annotations.
Not able to execute tests with #Test annotation when my test extends TestCase(Junit) in Eclipse
JUnit confusion: use 'extends TestCase' or '#Test'?
And this post mentions "In JUnit 4 the test classes no longer extend a common framework class. So there's no inherited method getName any more"
How to obtain test case name in JUnit 4 at runtime?
I tried extending my test case from TestCase, and this did make the name be reported correctly, but this single change screwed up some of its lifecycle and the tests failed in bizzarre ways. My understanding is that when the JUnit runner sees your tests class is derived from TestCase is runs it as a JUnit 3 test case and would therefore ignore all my #Before, #After etc. annotations. I don't see the point in stepping back into JUnit 3 test mechanisms.
Well this is embarrassing, but it deserves to be said. I think I just answered my own question.
The answer seems to be one of the tasks attributes, enableTestListenerEvents, described here: http://ant.apache.org/manual/Tasks/junit.html
I had ignored it because the docs say "since Ant 1.8.2 - Ant 1.7.0 to 1.8.1 behave as if this attribute was true by default." and I'm running 1.8.2, but I'd misread it, it's actually saying that in 1.8.2 it's off by default.
So the fix is to change my task in build.xml:
<junit haltonfailure="true" printsummary="on" fork="yes" dir=".">
to
<junit haltonfailure="true" printsummary="on" fork="yes" dir="."
enableTestListenerEvents="true">
NetBeans now prints up-to-the-moment information in the Test Results pane, including test names, as the tests progress. The whole feedback experience is far richer. Tell your friends :)