Software Engineering Lab
Code Python based here for understanding purpose
Program : Unit Testing of a Function
Aim: Write a unit test to check a function that calculates factorial.
Program (factorial.py):
def factorial(n):
if n < 0:
return None
return 1 if n == 0 else n * factorial(n - 1)
Unit Test (test_factorial.py):
import unittest
from factorial import factorial
class TestFactorial(unittest.TestCase):
def test_positive(self):
self.assertEqual(factorial(5), 120)
def test_zero(self):
self.assertEqual(factorial(0), 1)
def test_negative(self):
self.assertIsNone(factorial(-3))
if __name__ == "__main__":
unittest.main()
Explanation: Verifies correctness of factorial function for positive, zero, and negative inputs.
Program : Boundary Value Analysis (BVA)
Aim: Apply BVA on student grade evaluation.
Program (grade.py):
def grade(marks):
if marks < 0 or marks > 100:
return "Invalid"
elif marks < 40:
return "Fail"
elif marks < 60:
return "Second Class"
elif marks < 75:
return "First Class"
else:
return "Distinction"
Test Cases (Boundary Values):
- Input = -1 → Invalid
- Input = 0 → Fail
- Input = 39 → Fail
- Input = 40 → Second Class
- Input = 59 → Second Class
- Input = 60 → First Class
- Input = 74 → First Class
- Input = 75 → Distinction
- Input = 100 → Distinction
- Input = 101 → Invalid
Program : Equivalence Partitioning
Aim: Test a login function using valid/invalid partitions.
Program (login.py):
def login(username, password):
if username == "admin" and password == "1234":
return True
else:
return False
Test Cases (Partitions):
- Valid input: ("admin", "1234") → Pass
- Invalid username: ("user", "1234") → Fail
- Invalid password: ("admin", "abcd") → Fail
- Both wrong: ("guest", "0000") → Fail
Program : Control Flow Graph (CFG) & Cyclomatic Complexity
Aim: Draw CFG and compute Cyclomatic Complexity for a given code.
Sample Code:
def max_of_three(a, b, c):
if a > b:
if a > c:
return a
else:
return c
else:
if b > c:
return b
else:
return c
END
Cyclomatic Complexity Formula: V(G) = E - N + 2
= 10-8+2 = 4
Draw CFG by own and understand