Created
August 4, 2017 03:39
-
-
Save krmahadevan/855b8816ccd9996ec69b06091fa3a3f4 to your computer and use it in GitHub Desktop.
Sample code for https://stackoverflow.com/q/45484794/679824
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.rationaleemotions.stackoverflow.qn45484794; | |
import org.testng.annotations.AfterMethod; | |
import java.util.LinkedList; | |
import java.util.List; | |
public abstract class AbstractTest { | |
public static List<String> messages = new LinkedList<>(); | |
@AfterMethod | |
public final void tearDown() { | |
messages.add(getMessage("tearDown")); | |
} | |
protected String getMessage(String prefix) { | |
return getClass().getName() + "." + prefix + "()"; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.rationaleemotions.stackoverflow.qn45484794; | |
import org.testng.annotations.BeforeMethod; | |
import org.testng.annotations.Test; | |
public class ClassA extends AbstractTest { | |
@BeforeMethod | |
public void setUp() { | |
throw new RuntimeException(); | |
} | |
@Test | |
public void test() { | |
this.messages.add(getMessage("test")); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.rationaleemotions.stackoverflow.qn45484794; | |
import org.testng.annotations.Test; | |
public class ClassB extends AbstractTest { | |
@Test | |
public void test() { | |
this.messages.add(getMessage("test")); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.rationaleemotions.stackoverflow.qn45484794; | |
import org.assertj.core.api.Assertions; | |
import org.testng.Assert; | |
import org.testng.TestNG; | |
import org.testng.annotations.Test; | |
import org.testng.xml.XmlClass; | |
import org.testng.xml.XmlSuite; | |
import org.testng.xml.XmlTest; | |
import java.io.File; | |
import java.util.Arrays; | |
import java.util.Collections; | |
import java.util.List; | |
public class TestRunner { | |
@Test | |
public void testMethod() { | |
XmlSuite xmlSuite = new XmlSuite(); | |
xmlSuite.setParallel(XmlSuite.ParallelMode.NONE); | |
xmlSuite.setThreadCount(1); | |
xmlSuite.setVerbose(3); | |
xmlSuite.setName("xmlSuite"); | |
XmlTest xmlTest = new XmlTest(xmlSuite); | |
xmlTest.setName("xmlTest"); | |
List<XmlClass> classes = Arrays.asList(new XmlClass(ClassA.class), new XmlClass(ClassB.class)); | |
xmlTest.setClasses(classes); | |
TestNG testNG = new TestNG(); | |
testNG.setXmlSuites(Collections.singletonList(xmlSuite)); | |
testNG.run(); | |
AbstractTest.messages.forEach(System.err::println); | |
List<String> expected = Arrays.asList( | |
ClassB.class.getName() + ".test()", | |
ClassB.class.getName() + ".tearDown()" | |
); | |
Assertions.assertThat(AbstractTest.messages).containsExactlyElementsOf(expected); | |
String file = TestNG.class.getProtectionDomain().getCodeSource().getLocation().getFile(); | |
Assert.assertEquals(new File(file).getParentFile().getName(), "6.11"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm using the current master branch caa18a81da5832c3f5ed9a258b37a8a58cc7adcc:
When I run the class one by one via IntelliJ,
ClassB
is working.When I run the class together with the suite via IntelliJ,
ClassB
is skipped.But when I run the class together via IntelliJ,
ClassB
is working.So I think there really something different between run by code and run by XML (which is not supposed to be, but...).
When I run the
TestRunner
: