1

Maven + Surefire + TestNG + Guice (最新の安定版) を使用しています

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 プロパティであるため、最初の試みは次の Surefire 構成を使用することでした。

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

しかし、今は Guice からこのプロパティを使用する必要があるため、何らかの方法で 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ますが、データがそこにあるかどうか、または必要なことを行うためのより簡単な方法があるかどうかはわかりません。

返信ありがとうございます。

4

2 に答える 2

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を使用していませんが、それがあなたのコードと私のコードの唯一の違いです。

問題を再現する小さなMavenプロジェクトを自由に作成し、コンパイルしてから電子メールで送信できることを確認してください。

于 2011-06-08T18:31:49.430 に答える
-1

以下のテストを試してみましたが、正常に動作します

私の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);
    }
}
于 2012-11-29T17:06:15.710 に答える