Issue
I'm using Espresso to build UI tests. For some test cases I would like to call a specific after step to reset the state in case the script fails.
Is there a way to perform an @After
step for a single JUnit test case (@Test
)?
The only solution I can think of is to make a separate test class. But I would like the test cases to be grouped in the same test class.
Solution
It does sound a little odd ;) but ...
You could add a try/finally to the single test for which you want this after behaviour. For example:
@Test public void testA() { try { // the body of testA } finally { // apply the 'after' behaviour for testA } }
Or, if you really want to use JUnit's
@After
then you could use the TestName Rule (since JUnit 4.7) as follows:@Rule public TestName testName = new TestName(); @After public void conditionalAfter() { if ("testB".equals(testName.getMethodName())) { System.out.println("apply the 'after' behaviour for testB"); } } @Test public void testA() { } @Test public void testB() { }
Answered By - glytching
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.