TestNg,Junit對(duì)于我們測(cè)試來(lái)說(shuō)并不陌生,要運(yùn)行批量腳本的時(shí)候上面框架就起到了很大的作用。我在開(kāi)發(fā)測(cè)試工具的時(shí)候饱狂,發(fā)現(xiàn)開(kāi)發(fā)出來(lái)的工具只能單獨(dú)運(yùn)行一個(gè)腳本,這樣就失去了做自動(dòng)化的意義宪彩。對(duì)此我將TestNg框架集成到我的測(cè)試工具里面休讳,從而實(shí)現(xiàn)在java界面中實(shí)現(xiàn)腳本的批量運(yùn)行,首先在eclipse安裝好testNg插件
先附上效果圖
下面是代碼:
1.先編寫(xiě)4個(gè)簡(jiǎn)單的用例
package One;
import org.testng.annotations.Test;
public class NewTest1 {
@Test
public void f() {
System.out.println("1");
}
}
package One;
import org.testng.annotations.Test;
public class NewTest2 {
@Test
public void f() {
System.out.println("2");
}
}
package One;
import org.testng.annotations.Test;
import com.beust.testng.TestNG;
public class NewTest3 {
@Test
public void f() {
System.out.println("3");
}
}
package One;
import org.testng.annotations.Test;
public class NewTest4 {
@Test
public void f() {
System.out.println("4");
}
}
2.編寫(xiě)testNg運(yùn)行類(lèi)尿孔,運(yùn)行List中的所有用例
package One;
import java.util.ArrayList;
import java.util.List;
import org.testng.TestNG;
import org.testng.xml.XmlClass;
import org.testng.xml.XmlSuite;
import org.testng.xml.XmlTest;
public class TestNgRunUtils {
public static void runTestNG(List testNames){
XmlSuite suite = new XmlSuite();
suite.setName("Test");
XmlTest test = new XmlTest(suite);
test.setName("TestAA");
List<XmlClass> classes = new ArrayList<XmlClass>();
//添加運(yùn)行的testNg用例名
for(int i=0;i<testNames.size();i++){
classes.add(new XmlClass(""+testNames.get(i)));
}
test.setXmlClasses(classes) ;
List<XmlSuite> suites = new ArrayList<XmlSuite>();
suites.add(suite);
TestNG tng = new TestNG();
tng.setXmlSuites(suites);
tng.run();
}
}
3.核心類(lèi)如下
package One;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
import javax.swing.table.DefaultTableModel;
public class JpanelTestCase extends JPanel {
JTable table;
JScrollPane jScrollPane;
DefaultTableModel tableModel;
JOptionPane optionPane;
Vector<String>vector=new Vector<>();
JCheckBox jc1=null;
JButton start_case;
List caseList;
public JpanelTestCase(){
setBorder(BorderFactory.createTitledBorder("分組框")); //設(shè)置面板邊框俊柔,實(shí)現(xiàn)分組框的效果,此句代碼為關(guān)鍵代碼
setBorder(BorderFactory.createLineBorder(Color.WHITE));
setBounds(0,0, 520,400);
setLayout(null);
try {
initView();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
addView();
clickListenser();
}
private void initView() throws Exception {
//tableModel=new DefaultTableModel(tablesValues,columnNames);
// TODO Auto-generated method stub
removeAll();
setVisible(true);
start_case=new JButton("執(zhí)行用例");
start_case.setBounds(200,365, 100, 30);
//用例編號(hào)
List list_id=new ArrayList<>();
list_id.add("001");
list_id.add("002");
list_id.add("003");
list_id.add("004");
//用例名
List list_casename=new ArrayList<>();
list_casename.add("NewTest1");
list_casename.add("NewTest2");
list_casename.add("NewTest3");
list_casename.add("NewTest4");
List list_devicesid=new ArrayList<>();
System.out.println(list_id);
int list_size=list_id.size();
//tableModel=new DefaultTableModel(new String[]{"用例Id","用例名稱(chēng)","用例設(shè)備"},list_size);
tableModel=new DefaultTableModel();
tableModel.setColumnIdentifiers(new String[]{"用例id","用例名稱(chēng)","選擇狀態(tài)"});
//int index;
System.out.println("list長(zhǎng)度:"+list_size);
for(int i=0;i<list_size;i++){
String id=null;
String name=null;
Boolean checkStuas=false;
jc1= new JCheckBox();
id=(String) list_id.get(i);
//vector.add(id);
name=(String)list_casename.get(i);
tableModel.addRow(new Object[]{id,name,checkStuas});
}
table=new JTable(tableModel);
//設(shè)置第三列為復(fù)選框
table.getColumnModel().getColumn(2).setCellEditor(new DefaultCellEditor(jc1));
jScrollPane=new JScrollPane(table);
jScrollPane.setBounds(10,10,500,350);
}
private void addView() {
// TODO Auto-generated method stub
add(jScrollPane);
add(start_case);
}
private void clickListenser() {
// TODO Auto-generated method stub
start_case.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new StartCase().start();
}
});
}
class StartCase extends Thread{
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
caseList=new ArrayList<>();
int r= table.getSelectedRow();
int c= table.getSelectedColumn();
int a=table.getRowCount();
//得到選中狀態(tài)為true的用例名放入到list
for(int x=0;x<a;x++){
Object value= table.getValueAt(x, 2);
// System.out.println(value);
if(value.equals(true)){
caseList.add("One."+table.getValueAt(x, 1));
}
}
System.out.println(caseList);
TestNgRunUtils.runTestNG(caseList);
}
}
}
4.主類(lèi)代碼如下
package One;
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
public class JFram1 extends JFrame {
public JFram1(){
setLayout(null);
setBounds(0,0, 550,450);
JpanelTestCase jpanelTestCase=new JpanelTestCase();
Container container=getContentPane();
container.add(jpanelTestCase);
setLayout(null);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String []args){
new JFram1();
}
}