How is this alternative to TestNg SoftAssert?

2020-05-10 04:09发布

问题:

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 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.

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 class:

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);
            }
        }
    }
}

Sample test:

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");
    }

}

Sample output:

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

回答1:

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.



标签: java testng