Performing Equivalence Class Testing using Pytest
Prerequisite – Equivalence Class Testing
To perform automated Equivalence Class Testing, we can make use of Pytest or Unittest Python libraries. In this article, we will use the Pytest library to execute test cases for a simple program.
Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.
Perform Equivalence Testing for a program that determines the type of triangle given the length of its three sides A, B, and C. The range of the length of the sides lies between 10 and 50(both inclusive).
Code in triangletype.py file :
Now, we need to write test cases for the above program using the Pytest library. Each test case is written in a separate function where we use pytest.raises function to check that the given input is valid or invalid. For the above program, we create 7 invalid classes and 1 valid class. The 7 invalid classes are :
- a < 10
- a > 50
- b < 10
- b > 50
- c < 10
- c > 50
- Given values of a, b, c cannot form a triangle
The 1 valid class is :
- 10 <= a, b, c <= 50
NOTE : The function name and the test file name should always start with the word ‘test’.
Code in test_triangletype_eq.py file :
To execute the above test cases, create two separate files triangletype.py and test_triangletype_eq.py in a single folder. To execute write the following command :
pytest -v shows the verbose output.
The output is shown below :
As we can see in the above output, all the 8 test cases passed. However, if we edit a test case, for example, if we change the value of c variable to 90 in the test_valid() test case in test_triangletype_eq.py file, then the test case fails :