TestNG - เรียกใช้แต่ละอินสแตนซ์จากโรงงาน TestNG เป็นการทดสอบแยกกัน

ฉันใช้ TestNG สำหรับการทดสอบอัตโนมัติพร้อมกับ ReportNG สำหรับการรายงาน ฉันใช้คลาสโรงงาน TestNG เพื่อให้อินพุตที่แตกต่างกันสำหรับการทดสอบของฉัน ปัญหาที่ฉันเผชิญที่นี่คืออินสแตนซ์การทดสอบทั้งหมดที่โรงงานดำเนินการภายใต้การทดสอบเดียวกัน และรายงานที่สร้างขึ้นจะแสดงสถานการณ์ทั้งหมดภายใต้การทดสอบครั้งเดียว

ฉันต้องการเรียกใช้อินสแตนซ์การทดสอบแต่ละรายการที่โรงงานจัดทำเป็นการทดสอบแยกต่างหาก มีวิธีการทำเช่นนี้หรือไม่? PFB การกำหนดค่า xml ของฉัน

<suite name="Default suite" parallel="classes">
<listeners>
         <listener class-name="org.uncommons.reportng.HTMLReporter" />
</listeners>
<test verbose="2" name="Default test" group-by-instances="true">
    <classes>
        <class name="com.test.factory.RAExcelFactory"/> 
    </classes>
  </test> <!-- Default test -->
</suite> <!-- Default suite -->

person Mallikarjun Pasunkili    schedule 25.05.2017    source แหล่งที่มา


คำตอบ (1)


ไม่ ไม่สามารถทำได้ในขณะนี้ใน TestNG

หรือคุณสามารถพิจารณาทำสิ่งต่อไปนี้แทนการใช้โรงงาน

  • อัปเกรดเป็นเวอร์ชัน TestNG ล่าสุด
  • สร้างการใช้งาน org.testng.IAlterSuiteListener และภายในนั้นรวมตรรกะเพื่อสร้างแท็ก <test> หลายแท็กโดยใช้ตรรกะอะไรก็ตามที่คุณมีภายในโรงงานของคุณ (ฉันเดาว่ามันใช้ประโยชน์จากกลไกที่ขับเคลื่อนด้วยข้อมูล)

บางอย่างเช่นด้านล่าง

คลาสทดสอบ

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

public class StudentTest {
    private int age;
    @BeforeClass
    @Parameters("age")
    public void setup(int age) {
        this.age = age;
    }

    @Test
    public void firstTest() {
        Assert.assertTrue(age >=0);
    }

    @Test(dependsOnMethods = "firstTest")
    public void secondTest() {
        Assert.assertTrue(age <= 125);
    }
}

การใช้งาน IAlterSuiteListener

import org.testng.IAlterSuiteListener;
import org.testng.xml.XmlClass;
import org.testng.xml.XmlSuite;
import org.testng.xml.XmlTest;

import java.util.ArrayList;
import java.util.List;

public class SuiteAlteringListener implements IAlterSuiteListener {
    @Override
    public void alter(List<XmlSuite> suites) {
        for (XmlSuite suite : suites) {
            List<XmlTest> tests = new ArrayList<>();
            Integer[] datum = getData();
            for (Integer data : datum) {
                XmlTest test = new XmlTest(suite);
                test.setName("test_" + data);
                test.addParameter("age", Integer.toString(data));
                test.getClasses().add(new XmlClass(StudentTest.class));
            }
        }
    }

    private Integer[] getData() {
        //Change this to your data provider implementation
        return new Integer[]{
                1, 2, 3
        };
    }
}

ไฟล์ xml ของชุดโปรแกรม

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="my_suite" parallel="false" verbose="3">
    <listeners>
        <listener class-name="com.rationaleemotions.stackoverflow.SuiteAlteringListener"/>
    </listeners>

</suite>

และนี่คือลักษณะของ testng-results.xml (รายงานอื่นๆ ก็จะมีรายละเอียดคล้ายกันเช่นกัน) ฉันแค่เลือกที่จะแนบการนำเสนอที่ง่ายที่สุด

<?xml version="1.0" encoding="UTF-8"?>
<testng-results skipped="0" failed="0" ignored="0" total="6" passed="6">
  <reporter-output>
  </reporter-output>
  <suite name="my_suite" duration-ms="10077" started-at="2017-05-27T07:49:36Z" finished-at="2017-05-27T07:49:46Z">
    <groups>
    </groups>
    <test name="test_1" duration-ms="24" started-at="2017-05-27T07:49:36Z" finished-at="2017-05-27T07:49:36Z">
      <class name="com.rationaleemotions.stackoverflow.StudentTest">
        <test-method status="PASS" signature="setup(int)[pri:0, instance:com.rationaleemotions.stackoverflow.StudentTest@61dc03ce]" name="setup" is-config="true" duration-ms="8" started-at="2017-05-27T13:19:36Z" finished-at="2017-05-27T13:19:36Z">
          <params>
            <param index="0">
              <value>
                <![CDATA[1]]>
              </value>
            </param>
          </params>
          <reporter-output>
          </reporter-output>
        </test-method> <!-- setup -->
        <test-method status="PASS" signature="firstTest()[pri:0, instance:com.rationaleemotions.stackoverflow.StudentTest@61dc03ce]" name="firstTest" duration-ms="2" started-at="2017-05-27T13:19:36Z" finished-at="2017-05-27T13:19:36Z">
          <reporter-output>
          </reporter-output>
        </test-method> <!-- firstTest -->
        <test-method status="PASS" signature="secondTest()[pri:0, instance:com.rationaleemotions.stackoverflow.StudentTest@61dc03ce]" name="secondTest" duration-ms="1" started-at="2017-05-27T13:19:36Z" depends-on-methods="com.rationaleemotions.stackoverflow.StudentTest.firstTest" finished-at="2017-05-27T13:19:36Z">
          <reporter-output>
          </reporter-output>
        </test-method> <!-- secondTest -->
      </class> <!-- com.rationaleemotions.stackoverflow.StudentTest -->
    </test> <!-- test_1 -->
    <test name="test_2" duration-ms="2" started-at="2017-05-27T07:49:41Z" finished-at="2017-05-27T07:49:41Z">
      <class name="com.rationaleemotions.stackoverflow.StudentTest">
        <test-method status="PASS" signature="setup(int)[pri:0, instance:com.rationaleemotions.stackoverflow.StudentTest@458ad742]" name="setup" is-config="true" duration-ms="0" started-at="2017-05-27T13:19:41Z" finished-at="2017-05-27T13:19:41Z">
          <params>
            <param index="0">
              <value>
                <![CDATA[2]]>
              </value>
            </param>
          </params>
          <reporter-output>
          </reporter-output>
        </test-method> <!-- setup -->
        <test-method status="PASS" signature="firstTest()[pri:0, instance:com.rationaleemotions.stackoverflow.StudentTest@458ad742]" name="firstTest" duration-ms="0" started-at="2017-05-27T13:19:41Z" finished-at="2017-05-27T13:19:41Z">
          <reporter-output>
          </reporter-output>
        </test-method> <!-- firstTest -->
        <test-method status="PASS" signature="secondTest()[pri:0, instance:com.rationaleemotions.stackoverflow.StudentTest@458ad742]" name="secondTest" duration-ms="0" started-at="2017-05-27T13:19:41Z" depends-on-methods="com.rationaleemotions.stackoverflow.StudentTest.firstTest" finished-at="2017-05-27T13:19:41Z">
          <reporter-output>
          </reporter-output>
        </test-method> <!-- secondTest -->
      </class> <!-- com.rationaleemotions.stackoverflow.StudentTest -->
    </test> <!-- test_2 -->
    <test name="test_3" duration-ms="2" started-at="2017-05-27T07:49:46Z" finished-at="2017-05-27T07:49:46Z">
      <class name="com.rationaleemotions.stackoverflow.StudentTest">
        <test-method status="PASS" signature="setup(int)[pri:0, instance:com.rationaleemotions.stackoverflow.StudentTest@66d2e7d9]" name="setup" is-config="true" duration-ms="0" started-at="2017-05-27T13:19:46Z" finished-at="2017-05-27T13:19:46Z">
          <params>
            <param index="0">
              <value>
                <![CDATA[3]]>
              </value>
            </param>
          </params>
          <reporter-output>
          </reporter-output>
        </test-method> <!-- setup -->
        <test-method status="PASS" signature="firstTest()[pri:0, instance:com.rationaleemotions.stackoverflow.StudentTest@66d2e7d9]" name="firstTest" duration-ms="0" started-at="2017-05-27T13:19:46Z" finished-at="2017-05-27T13:19:46Z">
          <reporter-output>
          </reporter-output>
        </test-method> <!-- firstTest -->
        <test-method status="PASS" signature="secondTest()[pri:0, instance:com.rationaleemotions.stackoverflow.StudentTest@66d2e7d9]" name="secondTest" duration-ms="0" started-at="2017-05-27T13:19:46Z" depends-on-methods="com.rationaleemotions.stackoverflow.StudentTest.firstTest" finished-at="2017-05-27T13:19:46Z">
          <reporter-output>
          </reporter-output>
        </test-method> <!-- secondTest -->
      </class> <!-- com.rationaleemotions.stackoverflow.StudentTest -->
    </test> <!-- test_3 -->
  </suite> <!-- my_suite -->
</testng-results>

สิ่งนี้จะได้ผลสำหรับคุณหรือไม่?

person Krishnan Mahadevan    schedule 27.05.2017
comment
ขอบคุณสำหรับการตอบกลับอย่างละเอียด ฉันไม่ทราบถึง IAlterSuiteListener ดังนั้นฉันจึงสร้างผู้รายงานใหม่โดยการแก้ไข HTMLReporter ใน ReportNG เพื่อสร้างผลการทดสอบใหม่สำหรับแต่ละอินสแตนซ์ตามชื่อการทดสอบ (วิธี getTestName จาก ITest Interface) และรวมไว้เป็น ผู้ฟังชุดทดสอบ ฉันใช้ java8 group by และการไตร่ตรองเพื่อให้บรรลุสิ่งเดียวกันและใช้งานได้! - person Mallikarjun Pasunkili; 27.05.2017
comment
มัลลิการ์ชุน ปาซุนกีลี: ฉันกำลังประสบปัญหาเดียวกัน คุณช่วยบอกรายละเอียดเกี่ยวกับวิธีที่คุณใช้ HTML Reporter ให้ฉันหน่อยได้ไหม ขอบคุณ. - person bvr; 17.03.2018
comment
Krishnan ฉันจะทำเช่นเดียวกันกับ @factory ซึ่งเป็นวัตถุ 2 มิติได้อย่างไร (จากแผ่นงาน .xls) - person bvr; 17.03.2018