请用java语言写一个数据驱动模式包含邮件发送、HTMLTestRuner生成测试报告、yaml配置文件读取、excel和csv、json文件读取,数据库连接的自动化测试工具类
时间: 2023-05-15 16:06:00 浏览: 90
非常感谢您的问题。以下是一个简单的 Java 数据驱动测试框架,它包含邮件发送、HTMLTestRuner 生成测试报告、yaml 配置文件读取、excel 和 csv、json 文件读取,以及数据库连接的自动化测试工具类:
```java
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.opencsv.CSVReader;
import com.opencsv.exceptions.CsvValidationException;
import com.testautomationguru.utility.Xls_Reader;
public class DataDrivenTest {
private static final String CONFIG_FILE = "config.yaml";
private static final String TEST_DATA_FILE = "test_data.xlsx";
private static final String TEST_DATA_SHEET = "test_data";
private static final String TEST_DATA_CSV = "test_data.csv";
private static final String TEST_DATA_JSON = "test_data.json";
private static final String DB_URL = "jdbc:mysql://localhost:3306/test";
private static final String DB_USER = "root";
private static final String DB_PASSWORD = "password";
private static final String MAIL_TO = "test@test.com";
private static final String MAIL_FROM = "test@test.com";
private static final String MAIL_SUBJECT = "Test Report";
private Properties config;
private Connection conn;
@BeforeClass
public void setUp() throws IOException, SQLException {
// Load configuration from YAML file
ObjectMapper mapper = new ObjectMapper();
config = mapper.readValue(new File(CONFIG_FILE), Properties.class);
// Connect to database
conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
}
@AfterClass
public void tearDown() throws SQLException {
// Close database connection
conn.close();
}
@Test(dataProvider = "testData")
public void test(String input1, String input2, String expectedOutput) {
// Perform test
String actualOutput = input1 + input2;
Assert.assertEquals(actualOutput, expectedOutput);
}
@DataProvider(name = "testData")
public Object[][] testData() throws IOException, CsvValidationException {
// Read test data from Excel file
Xls_Reader xls = new Xls_Reader(TEST_DATA_FILE);
int rows = xls.getRowCount(TEST_DATA_SHEET);
List<Object[]> testData = new ArrayList<Object[]>();
for (int i = 2; i <= rows; i++) {
String input1 = xls.getCellData(TEST_DATA_SHEET, "Input1", i);
String input2 = xls.getCellData(TEST_DATA_SHEET, "Input2", i);
String expectedOutput = xls.getCellData(TEST_DATA_SHEET, "ExpectedOutput", i);
testData.add(new Object[] { input1, input2, expectedOutput });
}
// Read test data from CSV file
CSVReader csv = new CSVReader(new FileInputStream(TEST_DATA_CSV));
String[] headers = csv.readNext();
while (true) {
String[] row = csv.readNext();
if (row == null) {
break;
}
String input1 = row[0];
String input2 = row[1];
String expectedOutput = row[2];
testData.add(new Object[] { input1, input2, expectedOutput });
}
// Read test data from JSON file
ObjectMapper mapper = new ObjectMapper();
Map<String, Object> json = mapper.readValue(new File(TEST_DATA_JSON), Map.class);
List<Map<String, String>> jsonTestData = (List<Map<String, String>>) json.get("test_data");
for (Map<String, String> row : jsonTestData) {
String input1 = row.get("Input1");
String input2 = row.get("Input2");
String expectedOutput = row.get("ExpectedOutput");
testData.add(new Object[] { input1, input2, expectedOutput });
}
return testData.toArray(new Object[testData.size()][]);
}
@Test(dependsOnMethods = "test")
public void sendTestReport() throws AddressException, MessagingException, IOException {
// Generate HTML test report
HTMLTestRunner.run();
// Send test report by email
String mailBody = "Please find attached the test report.";
String mailAttachment = HTMLTestRunner.getReportFile();
MailSender.sendMail(MAIL_TO, MAIL_FROM, MAIL_SUBJECT, mailBody, mailAttachment);
}
private Map<String, String> getConfig(String key) {
Map<String, String> result = new HashMap<String, String>();
String value = config.getProperty(key);
String[] parts = value.split(",");
for (String part : parts) {
String[] keyValue = part.split(":");
result.put(keyValue[0], keyValue[1]);
}
return result;
}
private ResultSet executeQuery(String query) throws SQLException {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(query);
return rs;
}
}
```
请注意,此代码仅用于演示目的,您需要根据您的具体需求进行修改和扩展。