同时使用testNG和jUnit的诱惑力

问题描述 投票:1回答:1

可以在一个项目中使用testNG和JUnit的诱惑吗?

我有一个使用JUnit的旧项目,我想将项目移动到testNG,但我们应该支持JUnit上的旧测试。

我使用testNG - 6.8,JUnit - 4.12,诱惑 - 1.5.4,Maven。

我在pom.xml中创建了两个配置文件:'testNG'和'default' - 用于使用JUnit进行运行测试。

pom.hml

<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>

结果:testNG测试运行正常,生成了诱惑报告,但是当我运行jUnit测试时,当jenkins尝试创建报告时,我有下一个问题:

         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?

如果我从pom.xml testng中删除:

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

JUnit测试是有效的,但不可能使用testNG。

你能帮助在一个项目中使用testNG和JUnit的诱惑报告吗?

java junit testng allure
1个回答
0
投票

不太可能。

一个问题是您要求使用具有相同类名的依赖项(您可以),但您必须确保使用完全限定的导入。 I.E.而不是Assert使用org.testng.Assertorg.junit.Assert到位。但是,您必须始终如一地执行此操作,然后您必须仍然将每个库加载到其自己的类加载器中,以便您的类不会发生冲突。由于构建工具正在加载所有内容,因此它比简单的Java插件系统复杂得多,人们可以将相同的类名加载到一个JVM中。请参阅SO - Can you mix TestNG and JUnit Assertions together within the same Test / Framework?的答案。

编辑:如果JVM是每个构建,则只能将一个测试依赖项加载到类路径上。但您必须确保使用完全限定的域名(FQDN)导入。而不是Assert使用org.testng.Assertorg.junit.Assert并且需要有两组注释,或者你可以编写一个更改FQDN导入的转换包装器。 See this discussion about test - it relates您正在寻找一个非常具体案例的答案,答案可能是尝试一下。

  1. 首先,您可以创建三个或四个模块或项目,(常见),(testNg)和(Junit)。您可以将用于测试的静态代码放在一个公共测试中,也可以放在一个单独的库中。这样,测试单元只是对静态测试代码的引用。
  2. 另一种选择是将两个依赖项和两个注释放在一起,还是创建一个新的注释,它结合了两个注释?注释可以有注释。然后将测试任务重命名为非泛型,以便您可以在构建管道中指定要触发的测试任务。
© www.soinside.com 2019 - 2024. All rights reserved.