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

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.

Related

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.

'assert' is ignored on Junit Tests with InstrumentationTestCase

I set up JUnit Test from android unit testing support and get "FAIL" result from the following test class.
public class FooTest extends AndroidTestCase {
#Before
public void setUp() throws Exception { super.setUp(); }
#After
public void tearDown() throws Exception { super.tearDown(); }
#Test
public void testCase1() { assertTrue(false); }
}
However, after replacing 'AndroidTestCase' with 'InstrumentationTestCase', I got "SUCCESS" result in spite of containing assertion that obviously returns 'FAIL'.
I would like to get to know the reason why I had got such a result, which are different by super classes and how to use Context in JUnit framework tests.

Run single test within a test suite in Eclipse

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

running #Test one by one using reflection

I’m trying to run a package with many unit tests (one by one, not as a class) using reflection,
So when I get all the #Test methods that needs to be run I try to do
Result result = new JUnitCore().run(Request.method(Class
.forName(packageAndClass),getTestName()));
But the class returned in packageAndClass has
#Before, #BeforeClass methods (that also might be in its superclass)
So when running the code above I get all the tests running and fail(because some of their values are assigned in the #Before and #BeforeClass methods)
But when running it from eclipse (select the test method name -> right click -> run as -> Junit test)
They all pass (runing together or one by one)
Is there an api of Request that will run the before methods?
Why are you doing that? JUnit is supposed to run the tests for you!
I ran the following test with junit 4.9 :
public class RunOneTest {
public static void main(final String[] args) {
final Result result = new JUnitCore().run(Request.method(RunOneTest.class, "oneTest"));
System.out.println("result " + result.wasSuccessful());
}
#Test
public void oneTest() throws Exception {
System.out.println("oneTest");
}
#Test
public void anotherTest() throws Exception {
System.out.println("anotherTest");
}
#Before
public void before() {
System.out.println("before");
}
#BeforeClass
public static void beforeClass() {
System.out.println("beforeClass");
}
#After
public void after() {
System.out.println("after");
}
#AfterClass
public static void afterClass() {
System.out.println("afterClass");
}
}
and the output was :
beforeClass
before
oneTest
after
afterClass
result true
Are you really sure that the methods are not run ?

JUnit, Selenium and tearDown

Such a junit :
#Test
public void testA {
//some code here...
}
#Test
pulic void testB {
//some code here...
}
#After
public void closeBrowsers() throws Exception {
selenium.stop();
}
Here is the question : closeBrowsers() method called after every test method; in that case it is called twice and i got "Wrong test finished." from JUnit. I need a junit method/annotation which will be called after all tests finised (just called once after all tests finished), is it possible ?
Also i tried to check if selenium is up or not in closeBrowsers() but no way i couldn't find
any solution.
P.S : I 've read this one : How to close a browser on a selenium RC server which lost it's client
but i couldn't understand the solution and also currently http://www.santiycr.com.ar/djangosite/blog/posts/2009/aug/25/close-remaining-browsers-from-selenium-rc blog side is down
You can make your selenium variable static, initialize it in #BeforeClass static method and cleanup in #AfterClass:
public class ...... {
private static Selenium selenium;
#BeforeClass
public static void initSelenium() {
selenium = new DefaultSelenium(...); // or initialize it in any other way
}
#Test
public void testA {...}
#Test
pulic void testB {...}
#AfterClass
public static void closeBrowsers() throws Exception {
selenium.stop();
}
}
Use the #AfterClass annotation.
http://junit.sourceforge.net/doc/faq/faq.htm#organize_3

Categories

Resources