TestNG - เสียบกับ Eclipse
ในการตั้งค่า TestNG ด้วย Eclipse ให้ทำตามขั้นตอนด้านล่าง -
ขั้นตอนที่ 1: ดาวน์โหลด TestNG Archive
ดาวน์โหลดไฟล์ jar TestNG เวอร์ชันล่าสุดจาก http://www.testng.org
ระบบปฏิบัติการ | ชื่อที่เก็บถาวร |
---|---|
Windows | testng-6.8.jar |
ลินุกซ์ | testng-6.8.jar |
Mac | testng-6.8.jar |
เราถือว่าคุณได้คัดลอกไฟล์ JAR ด้านบนในโฟลเดอร์ C: \> TestNG
ขั้นตอนที่ 2: ตั้งค่าสภาพแวดล้อม Eclipse
เปิด eclipse →คลิกขวาที่โปรเจ็กต์และไปที่ property → Build Path → Configure Build Path และเพิ่ม testng-6.8.jar ในไลบรารีโดยใช้ปุ่มAdd External Jar
![](https://post.nghiatu.com/assets/tutorial/testng/images/eclipse_properties.jpg)
เราถือว่า Eclipse ของคุณมีปลั๊กอิน TestNG ในตัว หากไม่สามารถใช้งานได้โปรดรับเวอร์ชันล่าสุดโดยใช้ไซต์อัปเดต
ใน Eclipse IDE ของคุณเลือกการปรับปรุงความช่วยเหลือ / Software / ค้นหาและติดตั้ง
ค้นหาคุณสมบัติใหม่ที่จะติดตั้ง
ไซต์ระยะไกลใหม่
สำหรับ Eclipse 3.4 ขึ้นไปให้ป้อน http://beust.com/eclipse.
สำหรับ Eclipse 3.3 และต่ำกว่าให้ป้อน http://beust.com/eclipse1.
ตรวจสอบให้แน่ใจในช่อง URL ที่มีการตรวจสอบและคลิกถัดไป
จากนั้น Eclipse จะแนะนำคุณตลอดกระบวนการ
ตอนนี้ Eclipse ของคุณพร้อมแล้วสำหรับการพัฒนากรณีทดสอบ TestNG
ขั้นตอนที่ 3: ตรวจสอบการติดตั้ง TestNG ใน Eclipse
สร้างโครงการ TestNGProject ใน Eclipse ที่ตำแหน่งใดก็ได้
สร้างคลาส MessageUtil เพื่อทดสอบในโครงการ
/*
* This class prints the given message on console.
*/
public class MessageUtil {
private String message;
//Constructor
//@param message to be printed
public MessageUtil(String message) {
this.message = message;
}
// prints the message
public String printMessage() {
System.out.println(message);
return message;
}
}
สร้างคลาสทดสอบ TestNGExample ในโปรเจ็กต์
import org.testng.Assert;
import org.testng.annotations.Test;
public class TestNGExample {
String message = "Hello World";
MessageUtil messageUtil = new MessageUtil(message);
@Test
public void testPrintMessage() {
Assert.assertEquals(message,messageUtil.printMessage());
}
}
โครงสร้างโครงการควรเป็นดังนี้ -
![](https://post.nghiatu.com/assets/tutorial/testng/images/proj_struct.jpg)
สุดท้ายตรวจสอบผลลัพธ์ของโปรแกรมโดยคลิกขวาที่โปรแกรมและเรียกใช้เป็น TestNG
ตรวจสอบผลลัพธ์
![](https://post.nghiatu.com/assets/tutorial/testng/images/test_run.jpg)