سؤال

لتشغيلها معا، هناك خيارات قليلة متاحة ولكني اخترت استخدام ملفات تعريف مختلفة ل Junit و Testng. ولكن الآن المشكلة هي الاستبعاد بما في ذلك حالات الاختبار.

نظرا لأنه إذا أضفنا التبعية TESTNG إلى المشروع الرئيسي في Maven، فسوف تخطي جميع Junit، قررت وضعه في ملف تعريف منفصل. لذلك أنا أستثني اختبارات TESTNG في ملف التعريف الافتراضي (الرئيسية) من الترجمة باستخدام الإدخال التالي في Pom.xml:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.0.2</version>
        <configuration>
        <source>1.5</source>
        <target>1.5</target>
        <testExcludes>
            <testExclude>**/tests/**.*</testExclude>
            <testExclude>**/tests/utils/**.*</testExclude>
        </testExcludes>
    </configuration>
</plugin>

ونفس الشيء للحصول على البرنامج المساعد Surefire. لذلك يعمل بشكل جيد مع الملف الشخصي الرئيسي وينفذ اختبارات Junit4 فقط. ولكن عندما أستخدم الملف الشخصي Testng، لن يتم تنفيذ اختبار TESTNG حتى أنه لن يترجم منه. أنا أستخدم ملف التعريف التالي لتنفيذها.

<profile>
    <id>testNG</id>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.5</source>
                    <target>1.5</target>
                    <testIncludes>
                        <testInclude>**/tests/**.java</testInclude>
                        <testInclude>**/tests/utils/**.*</testInclude>
                    </testIncludes>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <skip>false</skip>
                    <includes>
                        <include>**/**.class</include>
                        <include>**/tests/utils/**.class</include>
                    </includes>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>5.8</version>
            <scope>test</scope>
            <classifier>jdk15</classifier>
        </dependency>
    </dependencies>
</profile>

أي شخص لديه أي فكرة لماذا لا يشملها وتجميع مرة أخرى؟

هل كانت مفيدة؟

المحلول

تكوين البرنامج المكون من برنامج التحويل البرمجي يستبعد أنواع TESTNG. يتم دمج التكوين من الملف الشخصي مع التكوين الافتراضي، لذلك تكوين التحويل البرمجي الفعال هو:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <configuration>
    <source>1.5</source>
    <target>1.5</target>
    <testIncludes>
      <testInclude>**/tests/**.java</testInclude>
      <testInclude>**/tests/utils/**.*</testInclude>
    </testIncludes>
    <testExcludes>
      <testExclude>**/tests/**.*</testExclude>
      <testExclude>**/tests/utils/**.*</testExclude>
    </testExcludes>
  </configuration>
</plugin>

هذا يعني أن أنواع Testng الخاصة بك لم تجميعها على الإطلاق، وبالتالي لا تعمل.

إذا حددتu003Cexcludes> سيتم تجميع القسم في الملف الشخصي TETNG الذي ستجاوز الاستبعد الافتراضي وأنواع TESTNG الخاصة بك وتشغيلها. لا أستطيع أن أتذكر ما إذا كان الأمر يعمل مع علامة فارغة باستثناء (أيu003Cexcludes/> )، قد تضطر إلى تحديد شيء من هذا القبيل لضمان تجاوز التكوين الافتراضي.

    <testExcludes>
      <testExclude>dummy</testExclude>
    </testExcludes>

نصائح أخرى

أبسط الحل هو مثل هذا

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

مزيد من المعلومات حول هذا: خلط اختبار TESTNG و JUNIT في وحدة واحدة من Maven - 2013 Edition

لم أجد أي حل مشترك عبر الإنترنت لكل من Surefire و Failsafe. يتغير ملف POM أدناه لي.

  • تتم خدعة Surefire من خلال تحديد البرنامج المساعد للأطفال بشكل صريح لكل من Junit و Testng.
  • تتم خدعة Failsafe عن طريق تحديد إعدامين، ولكل منها تكوين فارغ، واحد ل Junit والآخر ل Testng.

كلا الحلول هي الخارقة التي وجدت عبر الإنترنت. أعتقد أن الرابط إلى خدعة Surefire في إجابة أخرى على هذا السؤال.

<plugins>
<plugin>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>${surefire.version}</version>
    <dependencies>
        <dependency>
            <groupId>org.apache.maven.surefire</groupId>
            <artifactId>surefire-junit47</artifactId>
            <version>${surefire.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.maven.surefire</groupId>
            <artifactId>surefire-testng</artifactId>
            <version>${surefire.version}</version>
        </dependency>
    </dependencies>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>${failsafe.version}</version>
    <executions>
        <execution>
            <id>test-testng</id>
            <goals>
                <goal>integration-test</goal>
                <goal>verify</goal>
            </goals>
            <configuration>
                <testNGArtifactName>none:none</testNGArtifactName>
            </configuration>
        </execution>
        <execution>
            <id>test-junit</id>
            <goals>
                <goal>integration-test</goal>
                <goal>verify</goal>
            </goals>
            <configuration>
                <junitArtifactName>none:none</junitArtifactName>
            </configuration>
        </execution>
    </executions>
</plugin>           
<!-- ... -->
</plugins>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top