Domanda

In questo momento ho entrambi i tipi di test ma quando dico "test mvn" esegue solo test TestNG e non Junit. Voglio eseguire entrambi uno dopo l'altro. Qualche idea?

È stato utile?

Soluzione

Esiste un problema aperto per questi , quindi non esiste un modo elegante per farlo questo.

Sarebbe molto più semplice scegliere un framework e attenersi ad esso.

Modifica: la mia risposta precedente non funziona perché non è possibile specificare dipendenze nell'esecuzione. Ho provato alcuni approcci, ma il meglio che posso gestire è creare un profilo per la dipendenza TestNG in modo da poter alternare i test TestNG e JUnit, non sembra esserci un mezzo per eseguire i test TestNG e Junit 4 .

Un altro punto da notare: puoi avviare i tuoi test JUnit da TestNG , ma penso che questo funzioni solo per i test di JUnit 3.

Altri suggerimenti

Modo ufficiale con selezionando i provider .

  

Puoi anche specificare più fornitori come dipendenze, che verranno tutti eseguiti e produrranno un rapporto comune . Ciò può essere particolarmente utile con i fornitori esterni, poiché esistono pochi casi d'uso per combinare i fornitori inclusi.

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

Ulteriori informazioni al riguardo: Test di miscelazione di test GN e JUnit in un modulo Maven & # 8211; Edizione 2013

Link attuale per questo negli esempi di plug-in maven-surefire . Cerca " Esecuzione di TestNG e test JUnit " ;.

Dovrai configurare il provider testng affinché ignori i test junit in questo modo:

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

Ho un soluzione migliore .

L'idea è di creare due esecuzioni del maven-surefire-plugin , una per JUnit, una per TestNG. È possibile disabilitare uno di TestNG o JUnit per esecuzione specificando junitArtifactName o 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>

C'è un'altra via d'uscita per questo. Potresti chiedere a TestNG di eseguire anche casi di test Junit. Di seguito è riportato l'esempio testng.xml per eseguire tutti i casi di test

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

Grazie a questo link ( http://jira.codehaus.org/browse/SUREFIRE-377 ) , ecco una soluzione al mio problema precedente (con 3 esecuzioni invece di 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>

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

Allo stesso modo usa la dipendenza per TestNG quando richiesto

Ho scoperto che la soluzione era quella di forzare il plugin sure-fire a usare JUnit. L'ho fatto sovrascrivendo il plugin surefire nel progetto specifico come segue. La dipendenza costringe sicuramente a usare 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>

Basato su soluzioni precedenti. Ho trovato che ha funzionato meglio per noi. Un altro problema che stavamo affrontando era TestNG che cercava di eseguire vecchi test JUnit. Lo abbiamo evitato nominando tutti i test TestNG in modo diverso (ad es. * TestNG.java). Di seguito è riportata la configurazione con due esecuzioni di surefire-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>

Ho scoperto una soluzione per eseguire entrambi i tipi di test con TestNG senza modificare la configurazione dello strumento di creazione.

Ho provato con Gradle ma dovrei lavorare anche con Maven.

Nota che questo eseguirà i test JUnit all'interno di TestNG, ma non viceversa.

Il trucco è usare le annotazioni di entrambi i framework nelle classi di test e usare assert TestNG per la compatibilità con 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 questo hack, puoi facilmente eseguire test JUnit esistenti con TestNG, aiutandoti a migrarli quando il tempo lo consente.

Spero che sia d'aiuto!

per Junit questo ha risolto il mio 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 si specifica semplicemente il provider testng, eseguirà sia i test junit che i test testng una sola volta.
quindi non ci sono restrizioni sulla denominazione dei test.

versioni plug-in:
surefire-plugin 2.16 (entrambi i provider junit47 e testng sono impostati su 2.16)
testng dependency 6.8.7
junit dependency 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>
  

Di semplice esecuzione: mvn test -Pjunit-test (per eseguire il test basato su junit) o ??mvn test -PtestNG-test (per TestNG basato su test).

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top