java - 这个 TestNg SoftAssert 的替代方案如何?
问题描述
这是开发测试自动化框架的udemy 课程(来自“Lets Kode It”)。在课程中,教师CheckPoint
开发了一个类用于框架中。
CheckPoint
类似于TestNg
SoftAssert
。开发背后的动机CheckPoint
是它允许您自定义错误消息并提供有关测试方法中哪些验证点失败的更多信息。
我不确定CheckPoint
代码是否质量好,或者是否可以考虑将其合并到TestNg
库本身中。有人可以告诉我他们是否仅通过阅读代码就发现了任何重大问题,除了 api 不方便吗?
检查点类:
import org.testng.Assert;
import java.util.ArrayList;
import java.util.HashMap;
public class CheckPoint {
public static HashMap<String, String> resultMap = new HashMap<String, String>();
private static String PASS = "PASS";
private static String FAIL = "FAIL";
public static void clearHashMap() {
System.out.print("Clearing Results Hash Map");
resultMap.clear();
}
//Set status of the Result Map
private static void setStatus(String mapKey, String status) {
resultMap.put(mapKey, status);
System.out.println(mapKey + " :-> " + resultMap.get(mapKey));
}
/*
Keeps the verification point status with testName, Result and Verification Point Message in hash map
@param testName - The test case name
@param result - Verification Result from test method
@param resultMessage - Message tagged with verification
*/
public static void mark(String testName, boolean result, String resultMessage) {
testName = testName.toLowerCase();
String mapKey = testName + "." + resultMessage;
try {
if (result) {
setStatus(mapKey, PASS);
} else {
setStatus(mapKey, FAIL);
}
} catch (Exception e) {
System.out.println("Exception Occurred...");
setStatus(mapKey, FAIL);
e.printStackTrace();
}
}
/*
Keeps the verification point status with testName, Result and Verification Point Message in hash map
It asserts all the verifications in a test method, if any verification
in a test method is failed then the test case is failed
@param testName - The test case name
@param result - Verification Result from test method
@param resultMessage - Message tagged with verification
*/
public static void markFinal(String testName, boolean result, String resultMessage) {
testName = testName.toLowerCase();
String mapKey = testName + "." + resultMessage;
try {
if (result) {
setStatus(mapKey, PASS);
} else {
setStatus(mapKey, FAIL);
}
} catch (Exception e) {
System.out.println("Exception Occurred...");
setStatus(mapKey, FAIL);
e.printStackTrace();
}
ArrayList<String> resultList = new ArrayList<String>();
for (String key: resultMap.keySet()) {
resultList.add(resultMap.get(key));
}
for (int i = 0; i < resultList.size(); i++) {
if (resultList.contains(FAIL)) {
System.out.println("Test Method Failed");
Assert.assertTrue(false);
} else {
System.out.println("Test Method Successful");
Assert.assertTrue(true);
}
}
}
}
样品测试:
import CheckPoint;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class CheckpointTests {
@BeforeMethod
public void methodSetup(){
CheckPoint.clearHashMap();
}
@Test
public void test1(){
CheckPoint.mark("test1-step1", false, "test1 fail");
CheckPoint.mark("test1-step2", true, "test1 pass");
CheckPoint.markFinal("test1-step3", true, "test1 pass");
}
@Test
public void test2(){
CheckPoint.mark("test2-step1", true, "test2 pass");
CheckPoint.markFinal("test2-step2", true, "test2 pass");
}
}
样本输出:
Clearing Results Hash Map
test1-step1.test1 fail :-> FAIL
test1-step2.test1 pass :-> PASS
test1-step3.test1 pass :-> PASS
Test Method Failed
java.lang.AssertionError: expected [true] but found [false]
Expected :true
Actual :false
{long stack trace here!}
Clearing Results Hash Map
test2-step1.test2: pass :-> PASS
test2-step2.test2: pass :-> PASS
Test Method Successful
Test Method Successful
===============================================
Default Suite
Total tests run: 2, Passes: 1, Failures: 1, Skips: 0
解决方案
如果多个测试将访问将导致并发修改异常的映射,则您可能希望使用 ConcurrentHashMap,除了我没有特别看到任何错误。还使用像 slf4j 这样的记录器而不是 System.out.println。
推荐阅读
- django - 我将如何摆脱错误“经理”对象没有属性“活动”
- dynamic - 使用 istio 根据流量的错误响应将流量分流到两个版本
- c++ - 如何打印出 DECIMAL (tagDEC) 类型的值?
- r - 如何在R中双重集成一个函数
- r - 在 dplyr mutate/summarise 中应用函数和传播数据
- android - Android AppUpdateManager 未完成立即更新
- linux - 安装塔 RPM 任务失败
- android - Android 7 之前的 SwitchPreference 总是关闭
- android - 列出 Android Q / API 29 上的 WiFi 网络
- scala - 如何在微服务架构中从一台服务器下载多个文件并同时上传到另一台服务器(并发管道)