java调用main自动执行testng方法一
主方法
import com.com.utlis.createTestngXml;
import org.testng.TestNG;
import java.util.ArrayList;
import java.util.List;
/**
* @author lw
* @createTime 2019/3/30 15:35
* @description testng调试类
*/
public class inittet {
private static final String ESCAPE_PROPERTY = "org.uncommons.reportng.escape-output";
public static void main(String[] args) {
try {
//执行的类
createTestngXml.createTestngXml("com.com.javatest.one.domese");
} catch (Exception e) {
e.printStackTrace();
}
System.setProperty(ESCAPE_PROPERTY, "false");
List suites = new ArrayList();
System.out.println(suites);
suites.add(System.getProperty("user.dir") + "/mytestng.xml");
TestNG tng = new TestNG();
tng.setTestSuites(suites);
tng.run();
}
}
创建xml方法
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
/**
* @author lw
* @createTime 2019/3/30 16:12
* @description 自动生成tesgng.xml
*/
public class createTestngXml {
public static void createTestngXml(String classname) throws Exception {
Document document = DocumentHelper.createDocument();
Element root = DocumentHelper.createElement("suite");
document.setRootElement(root);
root.addAttribute("name", "automation");
root.addAttribute("parallel", "Automatically generate xml");
root.addAttribute("thread-count", String.valueOf("1"));
Element listeners = root.addElement("listeners"); //子标签
Element listener1 = listeners.addElement("listener");
listener1.addAttribute("class-name", "org.uncommons.reportng.HTMLReporter");
Element listener2 = listeners.addElement("listener");
listener2.addAttribute("class-name", "org.uncommons.reportng.JUnitXMLReporter");
Element test = root.addElement("test");
test.addAttribute("name", "127.0.0.1");
Element classes = test.addElement("classes");
Element classNode = classes.addElement("class");
classNode.addAttribute("name", classname);
OutputFormat format = new OutputFormat(" ", true);
XMLWriter xmlWrite2;
try {
xmlWrite2 = new XMLWriter(new FileOutputStream("mytestng.xml"), format);
xmlWrite2.write(document);
} catch (
UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (
FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (
IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("执行成功!!!");
}
}
自定义xml
测试@test
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import org.testng.annotations.Test;
import java.net.http.HttpResponse;
大同网站制作公司哪家好,找创新互联!从网页设计、网站建设、微信开发、APP开发、成都响应式网站建设等网站项目制作,到程序开发,运营维护。创新互联从2013年开始到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联。
/**
- @author lw
- @createTime 2019/3/30 16:12
- @description 自动生成tesgng.xml
*/
public class domese {
@Test
public void ss11() {
try {
var url = new URL("http://www.oracle.com/");
var conn = url.openConnection();
var reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
System.out.println(reader);
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void we() {
String uri = "http://www.baidu.com";
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(uri))
.GET()
.build();
HttpResponse response = null;
try {
response = client.send(request, HttpResponse.BodyHandlers.ofString());
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(response.body());
}
}
结果:
新闻名称:java调用main自动执行testng方法一
新闻来源:http://scjbc.cn/article/jpsses.html