Git Product home page Git Product logo

java_testng_mvn_example's Introduction

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");
        }

    }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.