1
votes

Is possible to use allure with testNG and JUnit in one project?

I have old project which use JUnit and I want to move project to testNG, but we should support old tests on JUnit.

I use testNG - 6.8, JUnit - 4.12, allure - 1.5.4, Maven.

I create in pom.xml 2 profiles: 'testNG' and 'default' - for run tests using JUnit.

pom.xml

<properties>
  <allure.version>1.5.4</allure.version>
  <aspectj.version>1.8.9</aspectj.version>
</properties>

<profiles>
  <profile>
     <id>default</id>
     <properties>
        <junit>true</junit>
        <listener>ru.yandex.qatools.allure.junit.AllureRunListener</listener>
      </properties>
  </profile>

  <profile>
     <id>testNG</id>
     <properties>
         <junit>false</junit>
     </properties>
     <dependencies>
       <dependency>
          <groupId>ru.yandex.qatools.allure</groupId>
          <artifactId>allure-testng-adaptor</artifactId>
          <version>${allure.version}</version>
       </dependency>
     </dependencies>
   </profile>
  </profiles>
   <dependencies>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.8</version>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>junit</groupId>
                    <artifactId>junit</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>ru.yandex.qatools.allure</groupId>
            <artifactId>allure-junit-adaptor</artifactId>
            <version>${allure.version}</version>
        </dependency>
        <dependency>
            <groupId>ru.yandex.qatools.allure</groupId>
            <artifactId>allure-maven-plugin</artifactId>
            <version>2.6</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>${aspectj.version}</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>${aspectj.version}</version>
        </dependency>
       </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19.1</version>
                <configuration>
                    <argLine>
                        -javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar"
                        -Xmx${heap_size}m
                    </argLine>
                    <properties>
                        <property>
                            <name>listener</name>
                            <value>${listener}</value>
                        </property>
                        <property>
                            <name>junit</name>
                            <value>${junit}</value>
                        </property>
                    </properties>
                    <reportsDirectory>${project.basedir}/target</reportsDirectory>
                    <excludes>
                        <exclude>${excludeTest}</exclude>
                    </excludes>
                    <failIfNoTests>false</failIfNoTests>
                    <runOrder>alphabetical</runOrder>
               </configuration>
            </plugin>
        </plugins>
    </build>

As the result: testNG tests run ok, allure report is generate, but when I run jUnit tests I have next problem when jenkins try to create report:

         Listener ru.yandex.qatools.allure.junit.AllureRunListener@4bc222e must be one of ITestListener, ISuiteListener, IReporter,  IAnnotationTransformer, IMethodInterceptor or IInvokedMethodListener
15:43:41 Usage: <main class> [options] The XML suite files to run
15:43:41   Options:
15:43:41     -configfailurepolicy               Configuration failure policy (skip or
15:43:41                                        continue)
15:43:41     -d                                 Output directory
15:43:41     -dataproviderthreadcount           Number of threads to use when running
15:43:41                                        data providers
15:43:41     -excludegroups                     Comma-separated list of group names to 
15:43:41                                        exclude
15:43:41     -groups                            Comma-separated list of group names to be
15:43:41                                        run
15:43:41     -junit                             JUnit mode
15:43:41                                        Default: false
15:43:41     -listener                          List of .class files or list of class
15:43:41                                        names implementing ITestListener or
15:43:41                                        ISuiteListener
15:43:41     -methods                           Comma separated of test methods
15:43:41                                        Default: []
15:43:41     -methodselectors                   List of .class files or list of class
15:43:41                                        names implementing IMethodSelector
15:43:41     -mixed                             Mixed mode - autodetect the type of
15:43:41                                        current test and run it with appropriate runner
15:43:41                                        Default: false
15:43:41     -objectfactory                     List of .class files or list of class
15:43:41                                        names implementing ITestRunnerFactory
15:43:41     -parallel                          Parallel mode (methods, tests or classes)
15:43:41     -port                              The port
15:43:41     -reporter                          Extended configuration for custom report
15:43:41                                        listener
15:43:41     -suitename                         Default name of test suite, if not
15:43:41                                        specified in suite definition file or source code
15:43:41     -suitethreadpoolsize               Size of the thread pool to use to run
15:43:41                                        suites
15:43:41                                        Default: 1
15:43:41     -testclass                         The list of test classes
15:43:41     -testjar                           A jar file containing the tests
15:43:41     -testname                          Default name of test, if not specified in
15:43:41                                        suitedefinition file or source code
15:43:41     -testnames                         The list of test names to run
15:43:41     -testrunfactory, -testRunFactory   The factory used to create tests
15:43:41     -threadcount                       Number of threads to use when running
15:43:41                                        tests in parallel
15:43:41     -usedefaultlisteners               Whether to use the default listeners
15:43:41                                        Default: true
15:43:41     -log, -verbose                     Level of verbosity
15:43:41     -xmlpathinjar                      The full path to the xml file inside the
15:43:41                                        jar file (only valid if -testjar was
15:43:41                                        specified)
15:43:41                                        Default: testng.xml
15:43:41 
15:43:41 
15:43:41 Results :
15:43:41 
15:43:41 Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
15:43:41 
15:43:41 [INFO] ------------------------------------------------------------------------
15:43:41 [INFO] Reactor Summary:
15:43:41 [INFO] 
15:43:41 [INFO] beans .............................................. SUCCESS [ 13.854 s]
15:43:41 [INFO] core ............................................... SUCCESS [  7.125 s]
15:43:41 [INFO] tests .............................................. FAILURE [ 29.277 s]
15:43:41 [INFO] API automation ..................................... SKIPPED
15:43:41 [INFO] ------------------------------------------------------------------------
15:43:41 [INFO] BUILD FAILURE
15:43:41 [INFO] ------------------------------------------------------------------------
15:43:41 [INFO] Total time: 50.396 s
15:43:41 [INFO] Finished at: 2019-03-26T12:43:41+00:00
15:43:42 [INFO] Final Memory: 73M/1479M
15:43:42 [INFO] ------------------------------------------------------------------------
15:43:42 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.19.1:test (default-test) on project tests: Execution default-test of goal org.apache.maven.plugins:maven-surefire-plugin:2.19.1:test failed: The forked VM terminated without properly saying goodbye. VM crash or System.exit called?

If I remove from pom.xml testng :

<dependency>
     <groupId>org.testng</groupId>
     <artifactId>testng</artifactId>
     <version>6.8</version>
 </dependency>

JUnit tests are work, but impossible use testNG.

Can you help to use allure reports for testNG and JUnit in one project?

1

1 Answers

0
votes

Not really possible.

One issue is that you are asking about using dependencies with same class names (Yes you can) but you have to make sure you use a fully qualified import. I.E. instead of Assert use org.testng.Assert or org.junit.Assert in place. However you must do this consistently and then you have to still load each library into its own class loader so that your classes do not clash. Since the build tools are loading everything it makes it much more complicated then a simple Java Plugin system, where people can load the same class names into one JVM. See SO - Can you mix TestNG and JUnit Assertions together within the same Test / Framework? for the answer.

Edit: If the JVM is per build you can load only one test dependency onto the classpath. But you have to make sure you use a fully qualified domain name (FQDN) import. Instead of Assert use org.testng.Assert or org.junit.Assert in place and would need to have two sets of annotations, alternately you could maybe write a translation wrapper that changes the FQDN import. See this discussion about test - it relates You are looking for an answer for a very specific case, the answer might be trying it out.

  1. To start you can create three or four modules or projects, (common), (testNg) and (Junit). You can place the static code for test in a common test, or a separate library all together. This way the test units are just references to static test code.

  2. The other option is to just put both dependencies and double of the annotations or create a new one, that combines your two annotations? Annotations can have annotations. Then rename the test task to be non-generic so you can specify in the build pipeline which test task to trigger.