Skip to content

ysmaoui/java_testng_mvn_example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JAVA/Testng simple example with maven

This is a simple project that has testng tests with requirements ( or any other information) can be added added as attributes in the test execution results.

Environment

Docker can be used to get a quick running environment that has java and maven setup.

The following command would open a bash terminal attached to a docker container with java and maven installed. the commands to generate the project and execute mvn commands can be executed from inside the docker container. all generated project files would be stored in the volume mapped to /workspace which is in this case the current working directory ( "$PWD")

    docker run -it --rm -v "$PWD":/workspace -w /workspace maven:3.6.0-jdk-11-slim bash

Project structure

The base project that was used here was generated ( Ref: [https://maven.apache.org/guides/getting-started/maven-in-five-minutes.html] )

mvn archetype:generate -DgroupId=com.example.app -DartifactId=testng_example -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false

Build

mvn package -DsuiteXmlFile=[testng_testsuite_file.xml]

run tests

mvn test -DsuiteXmlFile=[testng_testsuite_file.xml]

Definition of the suiteXmlFile option in the pom file

the option suiteXmlFile provides the possibility to specify the test suite that needs to be executed

        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
          <configuration>
            <!-- Suite testng xml file to consider for test execution -->
            <suiteXmlFiles>
              <suiteXmlFile>${suiteXmlFile}</suiteXmlFile>
            </suiteXmlFiles>
            ...
          </configuration>
        </plugin>

otherwise the exact file name can be specified in the pom file and we wouldn't need the option -DsuiteXmlFile to execute the tests

for example:

        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
          <configuration>
            <!-- Suite testng xml file to consider for test execution -->
            <suiteXmlFiles>
              <suiteXmlFile>testsuite_1.xml</suiteXmlFile>
              <suiteXmlFile>testsuite_2.xml</suiteXmlFile>
            </suiteXmlFiles>
            ...
          </configuration>
        </plugin>

Enabling the test attributes in the xml report

Ref: [testng-team/testng#1673 (comment)]

To enable test attributes in the test report the property generateTestResultAttributes needs to be set to true, this can be done with mvn in the pom file as follows:

    <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.22.1</version>
        <configuration>
        ...
        <properties>
            <property>
            <name>reporter</name>
            <value>org.testng.reporters.XMLReporter:generateTestResultAttributes=true,generateGroupsAttribute=true</value>
            </property>
        </properties>
        ...
        </configuration>
    </plugin>

To add attributes to a test:

Ref: [testng-team/testng#1673 (comment)]

    import org.testng.ITestResult;
    import org.testng.Reporter;
    import org.testng.annotations.Test;

    public class GithubIssue1673Sample {
        @Test(groups = "regression")
        public void testMethod1() {
            System.err.println("testMethod1() executed");
            ITestResult result = Reporter.getCurrentTestResult();
            result.setAttribute("name", "Krishnan");
        }
        @Test(groups = "regression")
        public void testMethod2() {
            System.err.println("testMethod2() executed");
            ITestResult result = Reporter.getCurrentTestResult();
            result.setAttribute("name", "Mahadevan");
        }

    }

About

a simple project with java testng and maven

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages