Run single test within a test suite in Eclipse - java

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");
}
}
}

Related

TestNg is running a test in a class after commenting #Test annotation

I have a class with list of tests annotated with #Test. I commented #Test annotation for one of the test however when I am running the suit the commented test is also running. Any idea on what's happening?
Example:
public class TestCasesClass {
#BeforeSuite
public void testSetup() throws Exception
{
super.testSetup();
}
#Test
public void test1() {
//Some test code
}
#Test
public void test12() {
//Some test code
}
//#Test
public void test3() {
//Some test code
}
}
When running the test suit all the test are running, including test3.
I tried #Test(enable-false) then also test running in the suit.

How to execute a piece of code once before all test classes start?

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.

Grails JUnit Test case, doesn't execute method marked with #BeforeClass annotation

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.

Using PowerMockRunner with Junit Test Suite

I'm trying to create a Junit test suite along with using PowerMockRunner but it does not work.
#RunWith(PowerMockRunner.class)
#PowerMockRunnerDelegate(MainTest.class)
#Suite.SuiteClasses({ MainTest.Class1Test.class })
#PrepareForTest({
StaticFieldsProvider.class
})
public class MainTest extends Suite {
public MainTest(Class<?> klass, RunnerBuilder builder)
throws InitializationError {
super(klass, builder);
}
public static class TestBase {
#Before
public void setUp() {
MockitoAnnotations.initMocks(this);
PowerMockito.mockStatic(StaticFieldsProvider.class);
}
}
public static class Class1Test extends TestBase {
#Before
public void setUp() {
super.setUp();
}
#Test
public void test(){
assertTrue(true);
}
}
}
When I try to run, it fails with error -
java.lang.IllegalArgumentException: Test class can only have one constructor
at org.junit.runners.model.TestClass.(TestClass.java:40)
Any suggestions on how to use PowerMockRunner in above case?
Thanks
This is an old question, so we may get no resolution on whether or not this solution works for the OP; but this might work (I can't verify without having access to StaticFieldsProvider, but it works if I swap that out with one of my own classes). I would love for someone to edit and add more explanation as to why this works:
#RunWith(PowerMockRunner.class)
// * Delegate to Suite.class instead of MainTest.class *
#PowerMockRunnerDelegate(Suite.class)
#Suite.SuiteClasses({ MainTest.Class1Test.class })
#PrepareForTest({
StaticFieldsProvider.class
})
// * Don't extend Suite *
public class MainTest {
// * Remove constructor *
public static class TestBase {
#Before
public void setUp() {
MockitoAnnotations.initMocks(this);
PowerMockito.mockStatic(StaticFieldsProvider.class);
}
}
public static class Class1Test extends TestBase {
#Before
public void setUp() {
super.setUp();
}
#Test
public void test(){
assertTrue(true);
}
}
}
In case it helps someone else, I had a slightly different scenario in that only a couple of the classes in my suite need PowerMockRunner (and don't mock out the same thing, so the mock needs to happen in each individual test class instead of in the runner). It appears that as long as I #PrepareForTest in my runner (as above) the classes I will need in some of the test classes, I can still create the mocks in the #Before (or wherever) of the applicable test class. Hope this helps.
You must not extend Suite, because this is a part of JUnit 3 and you are using JUnit 4. (Remove the extends and the constructor.) See the JUnit Wiki for more datails about Suites in JUnit 4.

TestNg's #BeforeTest on base class only happening once per fixture

I'm trying to use #BeforeTest to get code to ... run once before every test.
This is my code:
public class TestBase {
#BeforeTest
public void before() {
System.out.println("BeforeTest");
}
}
public class TestClass extends TestBase{
#Test
public void test1(){}
#Test
public void test2(){}
}
"BeforeTest" is only printed once, not twice. What am I doing wrong?
Use #BeforeMethod, not #BeforeTest.
The meaning of #BeforeTest is explained in the documentation.
"BeforeTest" is only printed once, not twice. What am I doing wrong?
***Sorry. I haven't noticed that you is written #BeforeTest , but in your example #BeforeTest almost equals #BeforeClass , and better to use #BeforeClass , when you haven't anymore test classes.
#BeforeClass" should be declared in same class that your tests methods, not differently!
//Example
package test;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class Tests {
private String bClass;
private String bMethod1;
private String bMethod2;
#BeforeClass
public void beforeClass() {
bClass = "BeforeClass was executed once for this class";
}
#BeforeMethod
public void beforeMetodTest1() {
bMethod1 = "It's before method for test1";
}
#Test
public void test1() {
System.out.println(bClass);
System.out.println(bMethod1);
}
#BeforeMethod
public void beforeMethodTest2() {
bMethod2 = "It's before method for test2";
}
#Test
public void test2() {
System.out.println(bClass);
System.out.println(bMethod2);
}
}
#BeforeClass will executed once, before your all tests methods in this class. #BeforeMethod will executed before test method, before which it is written.
#BeforeClass may be only one in test class, in difference #BeforeMethod!(If it is some #BeforeClass, they are carried out by turns, but it not a correct composition of the test)
P.S. Sorry for my English :)
According to documentation, a method annotated with #BeforeTest is run before any #Test method belonging to the classes inside the tag is run.
From my experience:
Each #BeforeTest method is run only once
If you have several #BeforeTest methods, the order of their execution depends on the order of the class containing those #BeforeTest method.
You could test this by setting up a simple example.
If you use #beforeTest, that method will be run once in the beginning of every <test> (we specify in the test suit xml file) if that test contains that class
All the #befortests within all the classes within a <test> will be executed at the beggining of that test
Official Documentations are so unclear and mostly ambiguous. This is why many don't like to read it. Not to mention no real-life examples shown.
If you are trying to get code to run once before every test, then you are looking for #BeforeMethod, not #BeforeTest. Because each of your tests independently considered methods.
#BeforeMethod runs before each method. I see you have two methods with the names test1() and test2(). Expect BeforeMethod to run before each of them.
#BeforeTest runs only once before ALL of your tests (test-methods). In Selenium, WebDriver is called once, this is better practice for testing.
#BeforeMethod will invoke WebDriver before each test, this is not good practice, especially when you have regression tests with hundreds of them to run.
For example, if you run this code (these are separate classes, not inner to each other, only showing for demonstration purposes here):
public class TestClass extends Base {
#Test
public void test1() {
System.out.println("Running Test 1");
}
#Test
public void test2() {
System.out.println("Running Test 2");
}
}
public class Base {
#BeforeTest
public void beforeTest() {
System.out.println("Before Test");
}
#AfterTest
public void afterTest() {
System.out.println("After Test");
}
#BeforeMethod
public void beforeMethod() {
System.out.println("Before Method");
}
#AfterMethod
public void afterMethod() {
System.out.println("After Method");
}
}
And you will get this as an output:
Before Test
Before Method
Running Test 1
After Method
Before Method
Running Test 2
After Method
After Test
As you can see BeforeMethods run before each test while BeforeTest and AfterTest run only once before and after completing ALL tests.
Hope this clarified the difference for you.

Categories

Resources