Pergunta

Agora eu tenho dois tipo de testes, mas quando digo "teste mvn" ele só executa testes TestNG e não JUnit. Eu quero executar ambos um após o outro. Alguma idéia?

Foi útil?

Solução

Há um problema aberto para thi s, por isso, não há nenhuma maneira elegante de fazer isso.

Seria muito mais simples para você escolher um quadro e ficar com ela.

Edit: A minha resposta anterior não funciona porque não é possível especificar dependências na execução. Eu tentei algumas abordagens, mas o melhor que pode gerenciar é criar um perfil para a dependência TestNG para que você possa alternar entre TestNG e teste JUnit, não parece ser um meio para executar os dois TestNG e JUnit 4 testes .

Um outro ponto a nota: Você pode lançar seus testes JUnit de TestNG , mas acho que isso só funciona para os testes JUnit 3.

Outras dicas

maneira oficial com provedores Seleção .

Você também pode especificar vários fornecedores como dependências, e todos eles serão executados e produzir um relatório comum . Isso pode ser especialmente útil com fornecedores externos, uma vez que existem poucos casos de uso para combinar os fornecedores incluídos.

<plugin>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.18.1</version>
    <dependencies>
        <dependency>
            <groupId>org.apache.maven.surefire</groupId>
            <artifactId>surefire-junit47</artifactId>
            <version>2.18.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.maven.surefire</groupId>
            <artifactId>surefire-testng</artifactId>
            <version>2.18.1</version>
        </dependency>
    </dependencies>
</plugin>

Mais informações sobre este: mistura testes TestNG e JUnit em um módulo Maven - 2013 edition

Current link para este efeito nos exemplos maven-infalível-plugin . Procure por " Running TestNG e JUnit Testes ".

Você vai querer configurar o provedor testng ignorar os testes JUnit assim:

<plugin>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.18.1</version>
    <configuration>        
    <properties>
        <property>
            <name>junit</name>
            <value>false</value>
         </property>
    </properties>        
    </configuration>
    [...providers as dependecies, see above...]
</plugin>

Eu tenho um melhor solução .

A idéia é criar duas execuções do maven-surefire-plugin, um para JUnit, um para TestNG. Você pode desativar um dos TestNG ou JUnit por execução, especificando não existente junitArtifactName ou testNGArtifactName:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <executions>
        <execution>
            <phase>test</phase>
            <goals>
                <goal>test</goal>
            </goals>
            <configuration> 
                <testNGArtifactName>none:none</testNGArtifactName>
            </configuration>
        </execution>
        <execution>
            <id>test-testng</id>
            <phase>test</phase>
            <goals>
                <goal>test</goal>
            </goals>
            <configuration> 
                <junitArtifactName>none:none</junitArtifactName>
            </configuration>
        </execution>
    </executions>
</plugin>

Há uma outra wayout para isso. Você poderia perguntar TestNG para executar casos de teste JUnit também. Abaixo está a testng.xml amostra para executar todos os casos de teste

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
 
<suite name="TestAll">
 
	<test name="junitTestCases" junit="true">
		<packages>
			<package name="com.test.*" />
			
		</packages>
	</test>
 
 <test name="testNGTestCases" >
		<packages>
			<package name="com.test.*" />
			
		</packages>
	</test>
</suite>

Graças a este link ( http://jira.codehaus.org/browse/SUREFIRE-377 ) , aqui está uma solução para o meu problema anterior (tendo 3 execuções em vez de 2)

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.10</version>
    <configuration>
       <testNGArtifactName>none:none</testNGArtifactName>
    </configuration>
    <executions>
       <execution>
          <phase>test</phase>
          <goals>
             <goal>test</goal>
          </goals>
          <configuration>
             <junitArtifactName>none:none</junitArtifactName>
             <testNGArtifactName>org.testng:testng</testNGArtifactName>
          </configuration>
       </execution>
    </executions>
</plugin>

Para JUnit ---

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-surefire-plugin</artifactId>
   <version>2.19.1</version>
   <dependencies>
  <dependency>
    <groupId>org.apache.maven.surefire</groupId>
    <artifactId>surefire-junit47</artifactId>
    <version>2.19.1</version>
  </dependency>
</dependencies>

Da mesma forma usar a dependência para TestNG quando necessário

Eu achei que a solução era forçar o certo-fogo de plugins de usar JUnit. Eu fiz isso, substituindo infalível plugin no projeto específico como segue. As forças de dependência infalível para uso JUnit.

<build>
    <plugins>
        <!-- force sure fire to use junit instead of testng -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.10</version>
            <dependencies>
                <dependency>
                    <groupId>org.apache.maven.surefire</groupId>
                    <artifactId>surefire-junit47</artifactId>
                    <version>2.10</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

Com base em soluções anteriores. Achei isso funcionou melhor para nós. Mais um problema que estavam enfrentando era TestNG tentando executar testes JUnit idade. Nós evitar este nomeando todos os testes TestNG de forma diferente (por exemplo * TestNG.java). Abaixo está a configuração com duas execuções de infalível-plugin.

    <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.12.4</version>
            <configuration>           
                <testNGArtifactName>none:none</testNGArtifactName>   
                <excludes>
                    <exclude>**/*TestNG.java</exclude>
                </excludes> 
            </configuration>
            <executions>
                <execution>
                    <id>test-testng</id>
                    <phase>test</phase>
                    <goals>
                        <goal>test</goal>
                    </goals>
                    <configuration> 
                        <junitArtifactName>none:none</junitArtifactName>          
                        <testNGArtifactName>org.testng:testng</testNGArtifactName>   
                        <includes>
                            <include>**/*TestNG.java</include>
                        </includes>
                        <excludes>
                            <exclude>**/*Test.java</exclude>
                        </excludes> 
                    </configuration>
                </execution>
            </executions>
        </plugin>

Eu descobri uma solução para executar os dois tipos de teste com TestNG sem alterar sua configuração de ferramenta de construção.

Eu testei com Gradle mas deve trabalhar com Maven também.

Note que isso irá executar testes JUnit dentro TestNG, mas não o outro caminho de volta.

O truque é usar anotações de ambas as estruturas nas classes de teste e usar TestNG afirma para compatibilidade JUnit.

import static org.testng.AssertJUnit.*;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

@org.testng.annotations.Test
public final class ApplicationTest {

    @org.testng.annotations.BeforeClass
    @BeforeClass
    public static void setup () {}

    @org.testng.annotations.AfterClass
    @AfterClass
    public static void cleanup () {}

    @Test public void json () throws IOException {
        assertTrue (true);
    }
}

Usando esse truque, você pode facilmente executar testes JUnit existentes com TestNG, ajudando você migrá-los quando o tempo permite.

Espero que ajude!

para JUnit esta resolveu o meu problema

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-surefire-plugin</artifactId>
   <version>2.19.1</version>
   <dependencies>
  <dependency>
    <groupId>org.apache.maven.surefire</groupId>
    <artifactId>surefire-junit47</artifactId>
    <version>2.19.1</version>
  </dependency>
</dependencies>

Se você especificar apenas provedor testng, ele será executado ambos os testes JUnit e testes TestNG tudo apenas uma vez.
então não há nenhuma restrição sobre nomear os testes.

plugins versões:
infalível-plugin 2,16 (prestadores junit47 e TestNG ambas as versões definidos para 2,16)
testng dependência 6.8.7
junit dependência 4.7

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.0</version>
            <configuration>
                <source>${java.version}</source>
                <target>${java.version}</target>
            </configuration>
        </plugin>
        <!-- ********* Skip Test for Success BUILD ******** -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.10</version>
            <configuration>
                <skip>true</skip>
            </configuration>
        </plugin>
        <!-- *********************************************** -->
    </plugins>
</build>

<profiles>
    <!-- ********** Profiles for run test cases ************ -->
    <!-- Profile for run JUnit test dependent tests -->
    <profile>
        <id>junit-tests</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.10</version>
                    <configuration>
                        <skip>false</skip>
                    </configuration>
                    <dependencies>
                        <dependency>
                            <groupId>org.apache.maven.surefire</groupId>
                            <artifactId>surefire-junit47</artifactId>
                            <version>2.10</version>
                        </dependency>
                    </dependencies>
                </plugin>
            </plugins>
        </build>
    </profile>

    <!-- Profile for run TestNG dependent tests -->
    <profile>
        <id>testNG-tests</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.10</version>
                    <configuration>
                        <skip>false</skip>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>

    <!-- ***************************************************** -->
</profiles>

Do que apenas executar:. Teste mvn -Pjunit-testes (para execução de teste com base em junit) ou teste mvn -PtestNG-testes (por teste TestNG base)

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top