Executing global teardown method after last test in junit4
I would like to know if there is a way to execute some method after executing my tests. I am using eclipse and i would like to be able to run some or all of my tests and then execute some code, so that junit will always execute my tear down once, always after the last test.
The concrete problem is that I am using selenium webdriver and i would like to execute webdriver's quit method after all my tests are done. First I had static class managing webdriver reference, now i moved to singleton pattern. I believe there could be better solution, but right now I don't want to use any IoC containers since I am java beginner.
开发者_StackOverflow社区So far i tried few approaches but none was successful.
Suit approach only works when i execute all my test:
@RunWith(Suite.class)
@SuiteClasses({SomeTest.class, OtherTest.class, AnotherTest.class})
public class TestSuit {
@BeforeClass
public static void setUp() {
System.out.println("setting up");
}
@AfterClass
public static void tearDown() {
System.out.println("tearing down");
}
}
but what if i would like to run just one test class or all tests from one specific namespace?
After class approach works only if all my test are grouped in one class.
I believe that NUnit has implemented SetUpFixtureAttribute , which is doing what i would like to gain, is there any counterpart in jUnit?
Have you considered using TestNG instead? You can use the @BeforeTest/@AfterTest or @BeforeSuite/@AfterSuite to run your browser open/close methods before/after a group of tests or suite of tests. (http://testng.org/doc/documentation-main.html)
< suite>< test>< classes>...< /classes>< /test>... < /suite>
Personally, I use the @BeforeMethod/@AfterMethod to load and close my browser, so I get a clean browser instance before each new tests. It takes longer of course to do this, but it's just a choice that I made based on the kind of tests/software I'm running. This also allows me to run my tests in parallel threads, without having to worry about the grouping of tests in my test classes if I were to use @BeforeClass/@Afterclass.
What you can do if you want to use JUnit in such a case is providing the alternative or altered version of the test runner that is used. Just set a debug flag inside your test case and debug what is happening after it. Sooner or later you hit the test runner implementation at the right spot.
Now is your time. Just copy the class into your src folder (you can use an additional src folder or inside your src/test/java, to not mess up your code base). Now alter the class (must be in the original package and with the original class name) in every way you like.
The fun part is that the class path is constructed (by eclipse / and others) that your classes are first to load by the bootstrap loader. This way your provided version is just picked up first before the original junit version has a chance to be loaded.
Now you can call something like a test runner extension (or whatever you want to call it) and have complete control over everything since you are in the same package as the original classes.
I for myself just extended it to order the methods the way they are declared within the class file (used asm to read the class file to get the correct ordering of methods).
Also I added behaviour to scan the package and sub packages for additional tests so I dont have to use suite or even give it some classes to build up. The sub suite are now constructed on the fly by discovering additional test cases. That is also true for my suite. Sometimes I need all tests in a particular sub package so I drop in a class annotated with suite.
That's all the rest does the runner by composing the suite. One might even add annotations to the game. So I annotate the tests like @Fast and run only fast tests within my test runner since the suite is also annotated as @Fast. You got it... .
精彩评论