maven 使用PaxExam 3.x运行测试- TestNg

2ledvvac  于 2023-03-29  发布在  Maven
关注(0)|答案(2)|浏览(131)

编辑以简化示例...
我正在将系统迁移到Maven。我想使用PaxExam通过TestNg运行测试。
我尝试使用PaxExam运行一个简单的测试:
pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>MyTest</groupId>
    <artifactId>PaxExam</artifactId>
    <version>0.0.2</version>
    <packaging>jar</packaging>
    <name>Prove PaxExam</name>

    <properties>
        <exam.version>3.3.0</exam.version>
        <url.version>1.6.0</url.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.ops4j.pax.exam</groupId>
            <artifactId>pax-exam-container-native</artifactId>
            <version>${exam.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.ops4j.pax.exam</groupId>
            <artifactId>pax-exam-testng</artifactId>
            <version>${exam.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.ops4j.pax.exam</groupId>
            <artifactId>pax-exam-link-mvn</artifactId>
            <version>${exam.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.ops4j.pax.url</groupId>
            <artifactId>pax-url-aether</artifactId>
            <version>${url.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.felix</groupId>
            <artifactId>org.apache.felix.framework</artifactId>
            <version>4.0.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
            <version>0.9.29</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>0.9.29</version>
            <scope>test</scope>
        </dependency>
            <dependency>
        <groupId>org.ow2.spec.ee</groupId>
        <artifactId>ow2-jta-1.1-spec</artifactId>
        <version>1.0.12</version>
        <scope>test</scope>
    </dependency>   
    </dependencies>
</project>

Java程序:

package MyTest.PaxExam;
    import static org.ops4j.pax.exam.CoreOptions.*;
    import static org.testng.Assert.*;
    import javax.inject.Inject;
    import org.ops4j.pax.exam.Option;
    import org.ops4j.pax.exam.Configuration;
    import org.ops4j.pax.exam.ConfigurationFactory;
    import org.ops4j.pax.exam.spi.reactors.PerClass;
    import org.ops4j.pax.exam.testng.listener.PaxExam;
    import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
    import org.osgi.framework.Bundle;
    import org.osgi.framework.BundleContext;
    import org.testng.annotations.Listeners;
    import org.testng.annotations.Test;
    import ch.qos.logback.classic.*;
    import org.slf4j.LoggerFactory;

    @Listeners(PaxExam.class)
    @ExamReactorStrategy(PerClass.class)
    public class Prove {

        @Inject
        BundleContext bc;

        @Configuration
        public Option[] config() {
            System.out.println("config() called");
            return options(mavenBundle("org.testng","testng","6.3.1"));         
        }

        @org.testng.annotations.Test
        public void checkInject() {
            System.out.println("checkInject() called");
            assertNotNull(bc);
        }   
    }

当我验证测试时,我得到的结果是:

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Prove PaxExam 0.0.2
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ PaxExam ---
[debug] execute contextualize
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\users\dan\desktop\acf\MyTest2\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ PaxExam ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-resources-plugin:2.5:testResources (default-testResources) @ PaxExam ---
[debug] execute contextualize
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ PaxExam ---
[INFO] Compiling 1 source file to C:\users\dan\desktop\acf\MyTest2\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.10:test (default-test) @ PaxExam ---
[INFO] Surefire report directory: C:\users\dan\desktop\acf\MyTest2\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------

Results :

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

[INFO]
[INFO] --- maven-jar-plugin:2.3.2:jar (default-jar) @ PaxExam ---
[INFO] Building jar: C:\users\dan\desktop\acf\MyTest2\target\PaxExam-0.0.2.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.246s
[INFO] Finished at: Fri Dec 06 16:33:14 CET 2013
[INFO] Final Memory: 15M/226M
[INFO] ------------------------------------------------------------------------

容器未启动,未运行任何测试。

xfb7svmp

xfb7svmp1#

不知道你想做什么。如果你想用嵌入式OSGi框架运行OSGi测试,不要使用exam-maven-plugin。
如果你试图运行与外部OSGi应用程序通信的普通的旧单元/集成测试,你可以使用exam-maven-plugin在Pax Exam server mode中启动该应用程序,但这样就不需要在测试中使用Pax Exam。

lxkprmvk

lxkprmvk2#

终于!
根据surefire插件文档:http://maven.apache.org/surefire/maven-surefire-plugin/examples/testng.html
只要它们是使用默认值命名的,比如 * Test.java,它们就会被Surefire作为TestNG测试运行。
如果要使用不同的命名方案,可以更改includes参数,如测试的Inclusions和Exclusions示例中所述。
我的测试类名既不是以Test开头,也不是以Test结尾。将类名更改为TestProve.java可以解决这个问题。
感谢hwellmann的时间。

相关问题