2011-06-08 77 views
1

我正在使用Maven + Surefire + TestNG + Guice(最新的穩定版本)如何將參數傳遞給Surefire Maven插件的guanted TestNG測試?

我有需要Guice運行的「大型」測試。 基本上我做這種方式:

@Test(groups = "large") 
@Guice(modules = FooLargeTest.Module.class) 
public class FooLargeTest { 
    public static class Module extends AbstractModule { 
    public void configure() { 
     bindConstant().annotatedWith(FooPort.class).to(5000); 
     // ... some other test bindings 
    } 
    } 
    @Inject Provider<Foo> fooProvider; 
    @Test 
    public void testFoo() { 
    Foo foo = fooProvider.get() // here injection of port is done 
           // it could not be passed to constructor 
    // ... actual test of foo 
    } 
} 

的問題是,FooPort被硬編碼到5000。這是一個Maven的屬性,因此在第一次嘗試是使用下一個神火配置:

<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-surefire-plugin</artifactId> 
    <configuration> 
     <suiteXmlFiles> 
     <suiteXmlFile>testng.xml</suiteXmlFile> 
     </suiteXmlFiles> 
     <systemPropertyVariables> 
     <fooPort>${foo.port}</fooPort> 
     </systemPropertyVariables> 
    </configuration> 
</plugin> 

這要求它像System.getProperty("fooPort")後。不幸的是,文檔說,這隻適用於JUnit測試。至少我在調試測試期間看不到這個系統變量。我試過forkMode默認的一個和never,它不會改變任何東西。對於TestNG進行它的建議做出這樣說:

<properties> 
    <property> 
    <name>fooPort</name> 
    <value>${foo.port}</value> 
    </property> 
</properties> 

但是現在我應該從吉斯使用此屬性,所以應該以某種方式給予GuiceModule,我已經試過了下一方式:

@Test(groups = "large") 
@Guice(moduleFactory = FooLargeTest.ModuleFactory.class) 
public class FooLargeTest { 
    public static class ModuleFactory extends AbstractModule { 
    private final String fooPort = fooPort; 
    @Parameters("fooPort") 
    public ModuleFactory(String fooPort) { 
     this.fooPort = fooPort; 
    } 
    public Module createModule(final ITestContext context, Class<?> testClass) { 
     return new AbstractModule { 
     public void configure() { 
      bindConstant().annotatedWith(FooPort.class).to(fooPort); 
      // ... some other test bindings 
     } 
     } 
    } 
    } 
    @Inject Provider<Foo> fooProvider; 
    @Test 
    public void testFoo() { 
    Foo foo = fooProvider.get() // here injection of port is done 
    // actual test of foo 
    } 
} 

但是這種方式也是失敗的,因爲modulefactories的創建者不考慮@Parameters,因此無法創建工廠實例。

看起來我應該嘗試從ITestContext context獲取一些數據,但我不知道數據是如何存在的,或者是否有一些簡單的方法來實現我想要的。

感謝您的回覆。

回答

2

我只是跑了一個快速測試和性能似乎正確地傳遞給TestNG的:

<configuration> 
     <suiteXmlFiles> 
     <suiteXmlFile>src/test/resources/testng-single.yaml</suiteXmlFile> 
     </suiteXmlFiles> 
     <systemPropertyVariables> 
     <foo>bar</foo> 
     </systemPropertyVariables> 

我的套房文件調用測試類B,其中包含:

@Test 
public void f() { 
    System.out.println(" property:" + System.getProperty("foo")); 
} 

,並運行它Maven顯示:

property:bar 
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.358 sec 

我沒有在這個簡單的例子中使用Guice,但那是唯一的d你的代碼和我的代碼之間的區別。

隨意創建一個複製您的問題的小Maven項目,確保我可以編譯它,然後通過電子郵件發送給我。

+0

貌似我能複製我的問題。這裏是縮小的項目。使用'mvn test'和'mvn -X test'運行項目的結果會被附上。 [附上郵編](http://narod.ru/disk/15469163001/maven-testng.zip.html) – 2011-06-09 07:06:13

0

我嘗試下面的測試和工程罰款

我的POM文件

<build> 
     <plugins> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-surefire-plugin</artifactId> 
       <version>2.12.4</version> 
       <configuration> 
        <!--<groups>Regression</groups> --> 
        <systemPropertyVariables> 
         <environment>UAT</environment> 
        </systemPropertyVariables> 
       </configuration> 
      </plugin> 
     </plugins> 
    </build> 

我TestNG的測試

import org.testng.annotations.Parameters; 
import org.testng.annotations.Test; 


public class TestAuthentication { 

    @Test (groups = { "Sniff", "Regression" }) 
    public void validAuthenticationTest(){ 
     System.out.println(" property:" + System.getProperty("environment")); 
    } 

    @Test (groups = { "Regression" },parameters = {"environment"}) 
    public void failedAuthenticationTest(String environment){ 
     System.out.println("Regression-"+environment); 
    } 

    @Parameters("environment") 
    @Test (groups = { "Sniff"}) 
    public void newUserAuthenticationTest(String environment){ 
     System.out.println("Sniff-"+environment); 
    } 
} 
相關問題