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

时间:2013-12-05 09:52:09

标签: maven testng pax-exam

编辑简化示例......

我正在将系统迁移到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] ------------------------------------------------------------------------

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

2 个答案:

答案 0 :(得分:0)

不确定你要做什么。如果您尝试使用嵌入式OSGi框架运行OSGi测试,请不要使用exam-maven-plugin。

如果您正在尝试运行与外部OSGi应用程序通信的普通旧单元/集成测试,您可以使用exam-maven-plugin在Pax Exam server mode中启动该应用程序,但之后无需使用考试中的Pax考试。

答案 1 :(得分:0)

最后!

根据surefire插件文档: http://maven.apache.org/surefire/maven-surefire-plugin/examples/testng.html

  

只要使用默认值(例如* Test.java)命名它们   将由Surefire作为TestNG测试运行。

     

如果您想使用不同的命名方案,可以更改   包括参数,如“包含和排除”中所述   测试示例。

我的测试类名称不以Test开头,也不以Test结束。将类名更改为TestProve.java可以解决问题。

感谢hwellmann的时间。