diff --git a/testng/pom.xml b/testng/pom.xml index 272b1d316d..83b32bb84d 100644 --- a/testng/pom.xml +++ b/testng/pom.xml @@ -1,115 +1,121 @@ - - - 4.0.0 - com.baeldung - testng - 0.1.0-SNAPSHOT - jar - testng - - - - - - - org.slf4j - slf4j-api - ${org.slf4j.version} - - - ch.qos.logback - logback-classic - ${logback.version} - - - - - - org.testng - testng - ${testng.version} - test - - - - - - testng - - - src/main/resources - true - - - - - src/main/resources - true - - - - - - - org.apache.maven.plugins - maven-compiler-plugin - ${maven-compiler-plugin.version} - - 1.8 - 1.8 - - - - - org.apache.maven.plugins - maven-surefire-plugin - ${maven-surefire-plugin.version} - - - **/*IntegrationTest.java - **/*LongRunningUnitTest.java - **/*ManualTest.java - - true - - - - - org.apache.maven.plugins - maven-dependency-plugin - - - copy-dependencies - prepare-package - - copy-dependencies - - - ${project.build.directory}/libs - - - - - - - - - - - - 1.7.21 - 1.1.7 - - - 6.10 - - - 3.6.0 - 2.19.1 - - - - + + + 4.0.0 + com.baeldung + testng + 0.1.0-SNAPSHOT + jar + testng + + + + + + + org.slf4j + slf4j-api + ${org.slf4j.version} + + + ch.qos.logback + logback-classic + ${logback.version} + + + + + + org.testng + testng + ${testng.version} + test + + + + + + testng + + + src/main/resources + true + + + + + src/main/resources + true + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + 1.8 + 1.8 + + + + + org.apache.maven.plugins + maven-surefire-plugin + ${maven-surefire-plugin.version} + + + **/*IntegrationTest.java + **/*LongRunningUnitTest.java + **/*ManualTest.java + + + src\test\resources\parametrized_testng.xml + src\test\resources\test_group.xml + src\test\resources\test_setup.xml + src\test\resources\test_suite.xml + + + true + + + + + org.apache.maven.plugins + maven-dependency-plugin + + + copy-dependencies + prepare-package + + copy-dependencies + + + ${project.build.directory}/libs + + + + + + + + + + + + 1.7.21 + 1.1.7 + + + 6.10 + + + 3.6.0 + 2.19.1 + + + + \ No newline at end of file diff --git a/testng/src/test/java/baeldung/com/DependentTests.java b/testng/src/test/java/baeldung/com/DependentTests.java index b60f8a6c0d..8d3bbfc11b 100644 --- a/testng/src/test/java/baeldung/com/DependentTests.java +++ b/testng/src/test/java/baeldung/com/DependentTests.java @@ -1,25 +1,25 @@ -package baeldung.com; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.testng.Assert; -import org.testng.annotations.Test; - -public class DependentTests { - - private static final Logger LOGGER = LoggerFactory.getLogger(DependentTests.class); - - private String email = "abc@qwe.com"; - - @Test - public void givenEmail_ifValid_thenTrue() { - boolean valid = email.contains("@"); - Assert.assertEquals(valid, true); - } - - @Test(dependsOnMethods = {"givenEmail_ifValid_thenTrue"}) - public void givenValidEmail_whenLoggedIn_thenTrue() { - LOGGER.info("Email {} valid >> logging in", email); - } -} - +package baeldung.com; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.testng.Assert; +import org.testng.annotations.Test; + +public class DependentTests { + + private static final Logger LOGGER = LoggerFactory.getLogger(DependentTests.class); + + private String email = "abc@qwe.com"; + + @Test + public void givenEmail_ifValid_thenTrue() { + boolean valid = email.contains("@"); + Assert.assertEquals(valid, true); + } + + @Test(dependsOnMethods = {"givenEmail_ifValid_thenTrue"}) + public void givenValidEmail_whenLoggedIn_thenTrue() { + LOGGER.info("Email {} valid >> logging in", email); + } +} + diff --git a/testng/src/test/java/baeldung/com/MultiThreadedTests.java b/testng/src/test/java/baeldung/com/MultiThreadedTests.java new file mode 100644 index 0000000000..4476eaa7a0 --- /dev/null +++ b/testng/src/test/java/baeldung/com/MultiThreadedTests.java @@ -0,0 +1,14 @@ +package baeldung.com; + +import org.testng.Assert; +import org.testng.annotations.Test; + +public class MultiThreadedTests { + + @Test(threadPoolSize = 5, invocationCount = 10, timeOut = 1000) + public void givenMethod_whenRunInThreads_thenCorrect() { + int count = Thread.activeCount(); + Assert.assertTrue(count > 1); + } + +} diff --git a/testng/src/test/java/baeldung/com/ParametrizedTests.java b/testng/src/test/java/baeldung/com/ParametrizedTests.java index d3813f5382..693a1b5cde 100644 --- a/testng/src/test/java/baeldung/com/ParametrizedTests.java +++ b/testng/src/test/java/baeldung/com/ParametrizedTests.java @@ -1,77 +1,77 @@ -package baeldung.com; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.testng.Assert; -import org.testng.annotations.DataProvider; -import org.testng.annotations.Parameters; -import org.testng.annotations.Test; - -public class ParametrizedTests { - - private static final Logger LOGGER = LoggerFactory.getLogger(ParametrizedTests.class); - - @Test - @Parameters({"value", "isEven"}) - public void givenNumberFromXML_ifEvenCheckOK_thenCorrect(int value, boolean isEven) { - Assert.assertEquals(isEven, value % 2 == 0); - } - - @DataProvider(name = "numbers") - public static Object[][] evenNumbers() { - return new Object[][]{{1, false}, {2, true}, {4, true}}; - } - - @Test(dataProvider = "numbers") - public void givenNumberFromDataProvider_ifEvenCheckOK_thenCorrect(Integer number, boolean expected) { - Assert.assertEquals(expected, number % 2 == 0); - } - - @Test(dataProvider = "numbersObject") - public void givenNumberObjectFromDataProvider_ifEvenCheckOK_thenCorrect(EvenNumber number) { - Assert.assertEquals(number.isEven(), number.getValue() % 2 == 0); - } - - @DataProvider(name = "numbersObject") - public Object[][] parameterProvider() { - return new Object[][]{{new EvenNumber(1, false)}, {new EvenNumber(2, true)}, {new EvenNumber(4, true),}}; - } - -} - - -class EvenNumber { - private int value; - private boolean isEven; - - public EvenNumber(int number, boolean isEven) { - this.value = number; - this.isEven = isEven; - } - - public int getValue() { - return value; - } - - public void setValue(int value) { - this.value = value; - } - - public boolean isEven() { - return isEven; - } - - public void setEven(boolean even) { - isEven = even; - } - - @Override - public String toString() { - return "EvenNumber{" + - "value=" + value + - ", isEven=" + isEven + - '}'; - } -} - - +package baeldung.com; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.testng.Assert; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Parameters; +import org.testng.annotations.Test; + +public class ParametrizedTests { + + private static final Logger LOGGER = LoggerFactory.getLogger(ParametrizedTests.class); + + @Test + @Parameters({"value", "isEven"}) + public void givenNumberFromXML_ifEvenCheckOK_thenCorrect(int value, boolean isEven) { + Assert.assertEquals(isEven, value % 2 == 0); + } + + @DataProvider(name = "numbers") + public static Object[][] evenNumbers() { + return new Object[][]{{1, false}, {2, true}, {4, true}}; + } + + @Test(dataProvider = "numbers") + public void givenNumberFromDataProvider_ifEvenCheckOK_thenCorrect(Integer number, boolean expected) { + Assert.assertEquals(expected, number % 2 == 0); + } + + @Test(dataProvider = "numbersObject") + public void givenNumberObjectFromDataProvider_ifEvenCheckOK_thenCorrect(EvenNumber number) { + Assert.assertEquals(number.isEven(), number.getValue() % 2 == 0); + } + + @DataProvider(name = "numbersObject") + public Object[][] parameterProvider() { + return new Object[][]{{new EvenNumber(1, false)}, {new EvenNumber(2, true)}, {new EvenNumber(4, true),}}; + } + +} + + +class EvenNumber { + private int value; + private boolean isEven; + + public EvenNumber(int number, boolean isEven) { + this.value = number; + this.isEven = isEven; + } + + public int getValue() { + return value; + } + + public void setValue(int value) { + this.value = value; + } + + public boolean isEven() { + return isEven; + } + + public void setEven(boolean even) { + isEven = even; + } + + @Override + public String toString() { + return "EvenNumber{" + + "value=" + value + + ", isEven=" + isEven + + '}'; + } +} + + diff --git a/testng/src/test/java/baeldung/com/RegistrationTest.java b/testng/src/test/java/baeldung/com/RegistrationTest.java index ec551d9c27..e9d744902b 100644 --- a/testng/src/test/java/baeldung/com/RegistrationTest.java +++ b/testng/src/test/java/baeldung/com/RegistrationTest.java @@ -1,14 +1,14 @@ -package baeldung.com; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.testng.annotations.Test; - -public class RegistrationTest { - private static final Logger LOGGER = LoggerFactory.getLogger(RegistrationTest.class); - - @Test - public void whenCalledFromSuite_thanOK() { - LOGGER.info("Registration successful"); - } -} +package baeldung.com; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.testng.annotations.Test; + +public class RegistrationTest { + private static final Logger LOGGER = LoggerFactory.getLogger(RegistrationTest.class); + + @Test + public void whenCalledFromSuite_thanOK() { + LOGGER.info("Registration successful"); + } +} diff --git a/testng/src/test/java/baeldung/com/SignInTest.java b/testng/src/test/java/baeldung/com/SignInTest.java index f0547374d1..d5cad78de0 100644 --- a/testng/src/test/java/baeldung/com/SignInTest.java +++ b/testng/src/test/java/baeldung/com/SignInTest.java @@ -1,14 +1,14 @@ -package baeldung.com; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.testng.annotations.Test; - -public class SignInTest { - private static final Logger LOGGER = LoggerFactory.getLogger(SignInTest.class); - - @Test - public void whenCalledFromSuite_thanOK() { - LOGGER.info("SignIn successful"); - } -} +package baeldung.com; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.testng.annotations.Test; + +public class SignInTest { + private static final Logger LOGGER = LoggerFactory.getLogger(SignInTest.class); + + @Test + public void whenCalledFromSuite_thanOK() { + LOGGER.info("SignIn successful"); + } +} diff --git a/testng/src/test/java/baeldung/com/SummationServiceTest.java b/testng/src/test/java/baeldung/com/SummationServiceTest.java index f377a009df..ceed53ca42 100644 --- a/testng/src/test/java/baeldung/com/SummationServiceTest.java +++ b/testng/src/test/java/baeldung/com/SummationServiceTest.java @@ -1,98 +1,98 @@ -package baeldung.com; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.testng.Assert; -import org.testng.TestNG; -import org.testng.annotations.*; - -import java.util.ArrayList; -import java.util.List; - -public class SummationServiceTest extends TestNG { - private static final Logger LOGGER = LoggerFactory.getLogger(DependentTests.class); - - private List numbers; - - private int testCount = 0; - - @BeforeClass - public void initialize() { - numbers = new ArrayList<>(); - } - - @AfterClass - public void tearDown() { - numbers = null; - } - - @BeforeSuite(groups = "regression") - public void runBeforeRegressionSuite() { - numbers = new ArrayList<>(); - numbers.add(-11); - numbers.add(2); - } - - @AfterSuite(groups = "regression") - public void runAfterRegressionSuite() { - numbers = null; - } - - @BeforeGroups("negative_tests") - public void runBeforeEachNegativeGroup() { - numbers.clear(); - } - - @BeforeGroups("regression") - public void runBeforeEachRegressionGroup() { - numbers.add(-11); - numbers.add(2); - } - - @BeforeGroups("positive_tests") - public void runBeforeEachPositiveGroup() { - numbers.add(1); - numbers.add(2); - numbers.add(3); - } - - @AfterGroups("positive_tests,regression,negative_tests") - public void runAfterEachGroup() { - numbers.clear(); - } - - @BeforeMethod - public void runBeforeEachTest() { - testCount++; - } - - @AfterMethod - public void runAfterEachTest() { - - } - - - @Test(groups = "positive_tests", enabled = false) - public void givenNumbers_sumEquals_thenCorrect() { - int sum = numbers.stream().reduce(0, Integer::sum); - Assert.assertEquals(sum, 6); - } - - @Test(groups = "negative_tests") - public void givenEmptyList_sumEqualsZero_thenCorrect() { - int sum = numbers.stream().reduce(0, Integer::sum); - Assert.assertEquals(0, sum); - } - - @Test(groups = "regression") - public void givenNegativeNumber_sumLessthanZero_thenCorrect() { - int sum = numbers.stream().reduce(0, Integer::sum); - Assert.assertTrue(sum < 0); - } - - @Test(expectedExceptions = ArithmeticException.class) - public void givenNumber_whenThrowsException_thenCorrect() { - int i = 1 / 0; - } - -} +package baeldung.com; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.testng.Assert; +import org.testng.TestNG; +import org.testng.annotations.*; + +import java.util.ArrayList; +import java.util.List; + +public class SummationServiceTest extends TestNG { + private static final Logger LOGGER = LoggerFactory.getLogger(DependentTests.class); + + private List numbers; + + private int testCount = 0; + + @BeforeClass + public void initialize() { + numbers = new ArrayList<>(); + } + + @AfterClass + public void tearDown() { + numbers = null; + } + + @BeforeSuite(groups = "regression") + public void runBeforeRegressionSuite() { + numbers = new ArrayList<>(); + numbers.add(-11); + numbers.add(2); + } + + @AfterSuite(groups = "regression") + public void runAfterRegressionSuite() { + numbers = null; + } + + @BeforeGroups("negative_tests") + public void runBeforeEachNegativeGroup() { + numbers.clear(); + } + + @BeforeGroups("regression") + public void runBeforeEachRegressionGroup() { + numbers.add(-11); + numbers.add(2); + } + + @BeforeGroups("positive_tests") + public void runBeforeEachPositiveGroup() { + numbers.add(1); + numbers.add(2); + numbers.add(3); + } + + @AfterGroups("positive_tests,regression,negative_tests") + public void runAfterEachGroup() { + numbers.clear(); + } + + @BeforeMethod + public void runBeforeEachTest() { + testCount++; + } + + @AfterMethod + public void runAfterEachTest() { + + } + + + @Test(groups = "positive_tests", enabled = false) + public void givenNumbers_sumEquals_thenCorrect() { + int sum = numbers.stream().reduce(0, Integer::sum); + Assert.assertEquals(sum, 6); + } + + @Test(groups = "negative_tests") + public void givenEmptyList_sumEqualsZero_thenCorrect() { + int sum = numbers.stream().reduce(0, Integer::sum); + Assert.assertEquals(0, sum); + } + + @Test(groups = "regression") + public void givenNegativeNumber_sumLessthanZero_thenCorrect() { + int sum = numbers.stream().reduce(0, Integer::sum); + Assert.assertTrue(sum < 0); + } + + @Test(expectedExceptions = ArithmeticException.class) + public void givenNumber_whenThrowsException_thenCorrect() { + int i = 1 / 0; + } + +} diff --git a/testng/src/test/java/baeldung/com/TestGroup.java b/testng/src/test/java/baeldung/com/TestGroup.java index 08bb5c996e..a592000bed 100644 --- a/testng/src/test/java/baeldung/com/TestGroup.java +++ b/testng/src/test/java/baeldung/com/TestGroup.java @@ -1,44 +1,44 @@ -package baeldung.com; - -import org.testng.annotations.AfterGroups; -import org.testng.annotations.BeforeGroups; -import org.testng.annotations.Test; - -public class TestGroup { - - @BeforeGroups("database") - public void setupDB() { - System.out.println("setupDB()"); - } - - @AfterGroups("database") - public void cleanDB() { - System.out.println("cleanDB()"); - } - - @Test(groups= "selenium-test") - public void runSelenium() { - System.out.println("runSelenium()"); - } - - @Test(groups= "selenium-test") - public void runSelenium1() { - System.out.println("runSelenium()1"); - } - - @Test(groups = "database") - public void testConnectOracle() { - System.out.println("testConnectOracle()"); - } - - @Test(groups = "database") - public void testConnectMsSQL() { - System.out.println("testConnectMsSQL"); - } - - @Test(dependsOnGroups = {"database","selenium-test"}) - public void runFinal() { - System.out.println("runFinal"); - } - +package baeldung.com; + +import org.testng.annotations.AfterGroups; +import org.testng.annotations.BeforeGroups; +import org.testng.annotations.Test; + +public class TestGroup { + + @BeforeGroups("database") + public void setupDB() { + System.out.println("setupDB()"); + } + + @AfterGroups("database") + public void cleanDB() { + System.out.println("cleanDB()"); + } + + @Test(groups = "selenium-test") + public void runSelenium() { + System.out.println("runSelenium()"); + } + + @Test(groups = "selenium-test") + public void runSelenium1() { + System.out.println("runSelenium()1"); + } + + @Test(groups = "database") + public void testConnectOracle() { + System.out.println("testConnectOracle()"); + } + + @Test(groups = "database") + public void testConnectMsSQL() { + System.out.println("testConnectMsSQL"); + } + + @Test(dependsOnGroups = {"database", "selenium-test"}) + public void runFinal() { + System.out.println("runFinal"); + } + } \ No newline at end of file diff --git a/testng/src/test/java/baeldung/com/TimeOutTest.java b/testng/src/test/java/baeldung/com/TimeOutTest.java index d54a914e08..f84367a972 100644 --- a/testng/src/test/java/baeldung/com/TimeOutTest.java +++ b/testng/src/test/java/baeldung/com/TimeOutTest.java @@ -1,11 +1,11 @@ -package baeldung.com; - -import org.testng.annotations.Test; - -public class TimeOutTest { - - @Test(timeOut = 1000, enabled = false) - public void givenExecution_takeMoreTime_thenFail() { - while (true) ; - } -} +package baeldung.com; + +import org.testng.annotations.Test; + +public class TimeOutTest { + + @Test(timeOut = 1000, enabled = false) + public void givenExecution_takeMoreTime_thenFail() { + while (true) ; + } +} diff --git a/testng/src/test/java/com/baeldung/reports/CustomisedListener.java b/testng/src/test/java/com/baeldung/reports/CustomisedListener.java new file mode 100644 index 0000000000..e8338c941d --- /dev/null +++ b/testng/src/test/java/com/baeldung/reports/CustomisedListener.java @@ -0,0 +1,67 @@ +package com.baeldung.reports; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.testng.ITestContext; +import org.testng.ITestListener; +import org.testng.ITestResult; + +public class CustomisedListener implements ITestListener { + private static final Logger LOGGER = LoggerFactory.getLogger("TEST_REPORT"); + + @Override + public void onFinish(ITestContext arg0) { + LOGGER.info("PASSED TEST CASES"); + arg0.getPassedTests() + .getAllResults() + .stream() + .forEach(result -> { + LOGGER.info(result.getName()); + }); + LOGGER.info("FAILED TEST CASES"); + arg0.getFailedTests() + .getAllResults() + .stream() + .forEach(result -> { + LOGGER.info(result.getName()); + }); + LOGGER.info("Test completed on: " + arg0.getEndDate().toString()); + } + + @Override + public void onStart(ITestContext arg0) { + LOGGER.info("Started testing on: " + arg0.getStartDate() + .toString()); + } + + @Override + public void onTestFailedButWithinSuccessPercentage(ITestResult arg0) { + // TODO Auto-generated method stub + + } + + @Override + public void onTestFailure(ITestResult arg0) { + LOGGER.info("Failed : " + arg0.getName()); + + } + + @Override + public void onTestSkipped(ITestResult arg0) { + LOGGER.info("Skipped Test: " + arg0.getName()); + + } + + @Override + public void onTestStart(ITestResult arg0) { + LOGGER.info("Testing: " + arg0.getName() + " " + arg0.getStartMillis()); + + } + + @Override + public void onTestSuccess(ITestResult arg0) { + LOGGER.info("Tested: " + arg0.getName() + " " + arg0.getEndMillis()); + + } + +} diff --git a/testng/src/test/java/com/baeldung/reports/CustomisedReports.java b/testng/src/test/java/com/baeldung/reports/CustomisedReports.java new file mode 100644 index 0000000000..07ba2162cc --- /dev/null +++ b/testng/src/test/java/com/baeldung/reports/CustomisedReports.java @@ -0,0 +1,50 @@ +package com.baeldung.reports; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.testng.IReporter; +import org.testng.ISuite; +import org.testng.ISuiteResult; +import org.testng.ITestContext; +import org.testng.xml.XmlSuite; + +import java.util.List; +import java.util.Map; + +public class CustomisedReports implements IReporter { + private static final Logger LOGGER = LoggerFactory.getLogger("TEST_REPORT"); + + @Override + public void generateReport(List xmlSuites, List suites, String outputDirectory) { + + suites.stream() + .forEach(suite -> { + String suiteName = suite.getName(); + Map suiteResults = suite.getResults(); + suiteResults.values() + .stream() + .forEach(result -> { + ITestContext context + = ((ISuiteResult) result).getTestContext(); + + LOGGER.info("Passed tests for suite '" + + suiteName + "' is:" + + context.getPassedTests() + .getAllResults() + .size()); + LOGGER.info("Failed tests for suite '" + + suiteName + "' is:" + + context.getFailedTests() + .getAllResults() + .size()); + LOGGER.info("Skipped tests for suite '" + + suiteName + "' is:" + + context.getSkippedTests() + .getAllResults() + .size()); + }); + }); + + } + +} diff --git a/testng/src/test/resources/logback.xml b/testng/src/test/resources/logback.xml index e9ae1894a6..346682f033 100644 --- a/testng/src/test/resources/logback.xml +++ b/testng/src/test/resources/logback.xml @@ -1,14 +1,14 @@ - - - - - web - %date [%thread] %-5level %logger{36} - %message%n - - - - - - - - + + + + + web - %date [%thread] %-5level %logger{36} - %message%n + + + + + + + + \ No newline at end of file diff --git a/testng/src/test/resources/parametrized_test.xml b/testng/src/test/resources/parametrized_testng.xml similarity index 74% rename from testng/src/test/resources/parametrized_test.xml rename to testng/src/test/resources/parametrized_testng.xml index 932af30e4e..c8ec7209f8 100644 --- a/testng/src/test/resources/parametrized_test.xml +++ b/testng/src/test/resources/parametrized_testng.xml @@ -1,10 +1,13 @@ - - - - - - - - - + + + + + + + + + + + + \ No newline at end of file diff --git a/testng/src/test/resources/test_group.xml b/testng/src/test/resources/test_group.xml index 26868375f2..77edb85cdc 100644 --- a/testng/src/test/resources/test_group.xml +++ b/testng/src/test/resources/test_group.xml @@ -1,13 +1,13 @@ - - - - - - - - - - - - + + + + + + + + + + + + \ No newline at end of file diff --git a/testng/src/test/resources/test_setup.xml b/testng/src/test/resources/test_setup.xml index 7d9708193e..9197f9fd0e 100644 --- a/testng/src/test/resources/test_setup.xml +++ b/testng/src/test/resources/test_setup.xml @@ -1,17 +1,17 @@ - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/testng/src/test/resources/test_suite.xml b/testng/src/test/resources/test_suite.xml index 0fe5d1cc40..0ccbbd2714 100644 --- a/testng/src/test/resources/test_suite.xml +++ b/testng/src/test/resources/test_suite.xml @@ -1,9 +1,12 @@ - - - - - - - - + + + + + + + + + + + \ No newline at end of file