I have two test suites with multiple test cases. Inside one of those test cases, I need to know the name of the caller test suite.
I tried to get it using getStackTrace() and Junit Rules(TestWatcher) but I did not get the test suite name.
// FirstTestSuite.java
#RunWith(Suite.class)
#SuiteClasses({ TestCase1.class, TestCase2.class })
public class FirstTestSuite {
}
// SecondTestSuite.java
#RunWith(Suite.class)
#SuiteClasses({ TestCase3.class, TestCase4.class })
public class SecondTestSuite {
}
// TestCase1.java
public class TestCase1 {
#Test
public void testTestCase1(){
//code
getTestSuiteName();
}
public void getTestSuiteName(){
// How can I get the test suite name which called me
}
}
Related
How can I execute a method once before all tests in all classes start?
I have a program that needs a system property to be set before any test start. Is there any way to do that?
Note: #BeforeClass or #Before are used just for the same test class. In my case, I'm looking for a way to execute a method before all test classes start.
To setup precondition to your test cases you can use something like this -
#Before
public void setUp(){
// Set up you preconditions here
// This piece of code will be executed before any of the test case execute
}
if you need to run that method before the start of all test you should use the annotation #BeforeClass or if you need to execute the same method every time you will execute a test method of that class you must use #Before
f.e
#Before
public void executedBeforeEach() {
//this method will execute before every single test
}
#Test
public void EmptyCollection() {
assertTrue(testList.isEmpty());
}
You can make use of a Test Suite.
The test suite
#RunWith(Suite.class)
#Suite.SuiteClasses({ TestClass.class, Test2Class.class, })
public class TestSuite {
#BeforeClass
public static void setup() {
// the setup
}
}
and, the test classes
public class Test2Class {
#Test
public void test2() {
// some test
}
}
public class TestClass {
#Test
public void test() {
// some test
}
}
Or, you can have a base class which handles the setup
public class TestBase {
#BeforeClass
public static void setup() {
// setup
}
}
and, then the test classes can extend the base class
public class TestClass extends TestBase {
#Test
public void test() {
// some test
}
}
public class Test2Class extends TestBase {
#Test
public void test() {
// some test
}
}
However, this will call the setup method in TestBase for all its subclasses everytime each of them executes.
Here's my code below, testSample() gets executed successfully. Please suggest what could possibly be wrong
class DataServiceTest extends GrailsUnitTestCase{
#BeforeClass
static void onceExecutedBeforeAll() {
println(" Print before Start Test Cases");
}
#Test
public void testSample(){
println(" Inside Sample");
}
}
You can't extend a TestCase and use annotations at the same time. If you want to create a test suite with annotations, you can use #RunWith annotation:
#RunWith(Suite.class)
#Suite.SuiteClasses({ DataServiceTest.class, OtherTest.class })
public class AllTests {
// empty
}
public class DataServiceTest { // no extends here
#BeforeClass
static void onceExecutedBeforeAll() {
println(" Print before Start Test Cases");
}
#Test
public void testSample(){
println(" Inside Sample");
}
}
Another option using JUnit could be annotating the method with #Before and removing extends GrailsUnitTestCase from the class.
I have some test casees declared following JUnit4 annotation #Test like below:
public class SampleTest {
#Test
public void execute() {
}
}
And I define a test suite declared like below:
public class FacadeMockTestSuite extends TestSuite {
public static Test suite() {
TestSuite suite = new TestSuite("My Test");
// ...
return suite;
}
}
When I tried to add my test case into the suite, I found I can only use the class of the case, through either #SuiteClass or suite.addTest(new JUnit4TestAdapter(myTestClass));
But how can I directly insert a test case instance created by myself into the suite? Since I need to scan test case classes in my runtime and create them with non-default constructor?
I always enjoyed running only one test of a test class. Now I'm using test suites to order my tests by tested methods into separate classes. However, Eclipse is not running the #BeforeClass-method if I want to run a single test of a test suite.
I have the following test setup:
#RunWith(Suite.class)
#SuiteClasses({ Test1.class, Test2.class })
public class TestSuite {
#BeforeClass
public static void setup (){
// essential stuff for Test1#someTest
}
public static class Test1{
#Test
public void someTest(){}
}
}
When I run someTest, it fails because TestSuite#setup isn't run. Is there a way to fix this?
If you just execute Test1, then JUnit doesn't know about TestSuite, so #BeforeClass is not picked up. You can add a #BeforeClass to Test1 that calls TestSuite.setup(). That will also require adding a static flag in TestSuite so it only executes once.
#RunWith(Suite.class)
#SuiteClasses({ Test1.class, Test2.class })
public class TestSuite {
private static boolean initialized;
#BeforeClass
public static void setup (){
if(initialized)
return;
initialized = true;
System.out.println("setup");
// essential stuff for Test1#someTest
}
public static class Test1{
#BeforeClass
public static void setup (){
TestSuite.setup();
}
#Test
public void someTest(){
System.out.println("someTest");
}
}
}
I am trying to use the Junit4 i using the below clases
public class Example extends TestCase {
public Example(String name) {
super(name);
}
#Test
public void exampletest() {
//TO DO
}
}
#RunWith(Suite.class)
#SuiteClasses({ Example.class })
public class Tests {
TestSuite tests = new TestSuite();
tests.addTest(new Example("exampletest"));
}
It gives me No tests found in junit4 exception some one can tell me why i get this exception
Or give an example how to do it?
In JUnit4, you don't make your test cases extend TestCase. But if you do, then your #Test annotations are ignored, and you have to prepend test method names by test.
Try this code:
Example.java:
import org.junit.Test;
public class Example {
#Test
public void exampletest() {
//TO DO
}
}
Tests.java:
#RunWith(Suite.class)
#SuiteClasses({ Example.class })
public class Tests {
}