这是 TestNg SoftAssert 的替代品吗? [英] How is this alternative to TestNg SoftAssert?

查看:21
本文介绍了这是 TestNg SoftAssert 的替代品吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个 udemy 课程(来自Lets KodeIt") 来开发测试自动化框架.在课程中,讲师开发了一个 CheckPoint 类以在框架中使用.

Here is a udemy course (from "Lets Kode It") to develop a test automation framework. In the course, a CheckPoint class has been developed by the instructor for use in the framework.

CheckPoint 类似于 TestNg SoftAssert.开发 CheckPoint 背后的动机是它允许您自定义错误消息并提供有关测试方法中哪些验证点失败的更多信息.

CheckPoint is similar to TestNg SoftAssert. The motivation behind developing CheckPoint is that it allows you to customize your error messages and gives more information as to which verification points in your test method failed.

我不确定 CheckPoint 代码是否具有良好的质量,或者是否可以考虑将其合并到 TestNg 库本身中.有人可以告诉我,除了 api 不方便之外,他们是否仅通过阅读代码就发现了任何重大问题?

I am not sure if CheckPoint code is of good quality or if it could even be considered for merging into the TestNg library itself. Can someone please tell me if they see any major issues in the code just by reading it, other than the api being inconvenient ?

CheckPoint 类:

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.

You might want to use ConcurrentHashMap if multiple tests will be accessing the map which will lead to concurrent modification exception, other than that i don't see anything wrong in particular. Also using loggers like slf4j instead of System.out.println.

这篇关于这是 TestNg SoftAssert 的替代品吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆