Prerequisite – BVA Testing
To perform automated BVA(Boundary Value Analysis) Testing, we can use Pytest or Unittest libraries in Python. Here, we will use the Pytest library to execute test cases written for a simple program.
We’ll be perform BVA Testing for a program that determines the type of the triangle (i.e. Equilateral, Isosceles, Scalene or If triangle formation is not possible) when the length of its sides A, B, and C are given. Also, the range of the length is between 10 to 50 (both inclusive).
Code in triangletype.py file:
Python3
# import math class Error(BaseException): pass class OutOfRangeError(Error): def __init__( self , message): self .message = message class TriangleError(Error): def __init__( self , message): self .message = message def checkRange(a, b, c): if a< 10 or a> 50 : raise OutOfRangeError( 'A is out of the given range' ) if b< 10 or b> 50 : raise OutOfRangeError( 'B is out of the given range' ) if c< 10 or c> 50 : raise OutOfRangeError( 'C is out of the given range' ) def checkTriangle(a, b, c): if a + b< = c or b + c< = a or c + a< = b: raise TriangleError( 'Triangle cannot be formed with these sides' ) def triangleType(a, b, c): checkRange(a, b, c) checkTriangle(a, b, c) # s = (a + b+c)/2 # ar = math.sqrt(s*(s-a)*(s-b)*(s-c)) # inradius = ar / s if (a = = b and b = = c): return "Equilateral Triangle" elif (a = = b or a = = c or b = = c): return "Isosceles Triangle" else : return "Scalene Triangle" def main(): try : print ( "Enter the sides of the triangle in range [10-50]" ) a = int ( input ( 'Enter Side A:' )) b = int ( input ( 'Enter Side B:' )) c = int ( input ( 'Enter Side C:' )) except ValueError as v: print (v + " Raised :Input is not an integer." ) exit( 0 ) try : checkRange(a, b, c) except OutOfRangeError as e: print ( "OutOfRangeError:" + e.message) try : checkTriangle(a, b, c) except TriangleError as e: print ( 'TriangleError:' + e.message) typeOfTriangle = triangleType(a, b, c) print ( "The triangle is: " + typeOfTriangle) if __name__ = = "__main__" : main() |
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. In BVA, there are 3 techniques used: Boundary Value Checking(BVC), Robustness Testing and Worst-Case Testing. Here, we will perform Robustness testing where for n number of input variables, 6n + 1 test cases can be written.
NOTE: The function name and the test file name should always start with the word ‘test’.
In the above program, n = 3 Since number of test cases = 6n + 1 Therefore, Number of test cases = 6 * 3 + 1 = 19 test cases
Hence, we write 19 test cases for the above program for the following values: min, min – 1, min + 1, max, max – 1, max + 1 and nominal value
Code in test_triangletype_bva.py file:
Python3
import pytest from triangletype import triangleType from triangletype import OutOfRangeError def test_bva_minm_a(): with pytest.raises(OutOfRangeError): triangleType( 9 , 20 , 15 ) def test_bva_minm_b(): with pytest.raises(OutOfRangeError): triangleType( 20 , 9 , 15 ) def test_bva_minm_c(): with pytest.raises(OutOfRangeError): triangleType( 20 , 15 , 9 ) def test_bva_min_a(): assert triangleType( 10 , 20 , 15 ) = = "Scalene Triangle" def test_bva_min_b(): assert triangleType( 20 , 10 , 15 ) = = "Scalene Triangle" def test_bva_min_c(): assert triangleType( 15 , 20 , 10 ) = = "Scalene Triangle" def test_bva_minp_a(): assert triangleType( 11 , 20 , 15 ) = = "Scalene Triangle" def test_bva_minp_b(): assert triangleType( 20 , 11 , 15 ) = = "Scalene Triangle" def test_bva_minp_c(): assert triangleType( 15 , 20 , 11 ) = = "Scalene Triangle" def test_bva_maxm_a(): assert triangleType( 49 , 30 , 45 ) = = "Scalene Triangle" def test_bva_maxm_b(): assert triangleType( 30 , 49 , 45 ) = = "Scalene Triangle" def test_bva_maxm_c(): assert triangleType( 45 , 30 , 49 ) = = "Scalene Triangle" def test_bva_max_a(): assert triangleType( 50 , 30 , 45 ) = = "Scalene Triangle" def test_bva_max_b(): assert triangleType( 30 , 50 , 45 ) = = "Scalene Triangle" def test_bva_max_c(): assert triangleType( 45 , 30 , 50 ) = = "Scalene Triangle" def test_bva_maxp_a(): with pytest.raises(OutOfRangeError): triangleType( 51 , 30 , 45 ) def test_bva_maxp_b(): with pytest.raises(OutOfRangeError): triangleType( 30 , 51 , 45 ) def test_bva_maxp_c(): with pytest.raises(OutOfRangeError): triangleType( 30 , 45 , 51 ) def test_bva_nominal(): assert triangleType( 12 , 12 , 12 ) = = "Equilateral Triangle" |
To execute the above test cases, create two separate files triangletype.py and test_triangletype_bva.py in a single folder. To execute write the following command:
pytest
OR
pytest -v
pytest -v shows verbose output.
The output is shown below:
As we can see in the output, all the 19 test cases have passed. However, if we edit the test cases in a way that the input is invalid, then the test cases will fail. For example, if we change the value of c variable in the test_bva_max_c() test case in test_triangletype_bva.py file, then the output comes out to be as follows: