PYTHON: UNIT TESTING

Diwakar pratap
4 min readMar 29, 2022

Unit Testing is that the initial level of software system testing wherever the tiniest testable components of a software system area unit tested. this can be wont to validate that every unit of the software system performs as designed.

Unit testing may be a technique during which specific module is tested to ascertain by developer himself whether or not there area unit any errors. the first focus of unit testing is testing a private unit of system to investigate, detect, and fix the errors.

Python provides the unittest module to check the unit of ASCII text file. The unittest plays a vital role once we area unit writing the large code, and it provides the power to ascertain whether or not the output is correct or not.

The unittest unit testing framework was originally galvanized by JUnit and contains a similar flavor as major unit testing frameworks in alternative languages. It supports check automation, sharing of setup and closure code for tests, aggregation of tests into collections, and independence of the tests from the news framework.

OOP ideas supported by unittest framework

• check fixture
• test case
• test suite
• check runner

1. test fixture
A check fixture is employed as a baseline for running tests to make sure that there’s a hard and fast setting during which tests area unit run in order that results area unit repeatable.

Examples :
creating temporary databases.
starting a server method.

2. test case
A legal action may be a set of conditions that is employed to work out whether or not a system underneath check works properly.

3. test suite
Test suite may be a assortment of checkcases that area unit wont to test a software system program to point out that it’s some such set of behaviors by capital punishment the mass tests along.

4. test runner
A check runner may be a part that originated the execution of tests and provides the result to the user.

Basic check Structure

import unittest
class SimpleTest(unittest.TestCase):
# Returns True or False.
def test(self):
self.assertTrue(True)
if __name__ == ‘__main__’:
unittest.main()

Outcomes potential
There area unit 3 forms of potential check outcomes:

• OK
• FAIL
• ERROR

1. OK
This means that each one the tests area unit passed.

2. FAIL
This means that the check failed to pass Associate in Nursingd an AssertionError exception is raised.

3. ERROR
This means that the check raises Associate in Nursing exception aside from AssertionError.

Basic terms utilized in the code :

• assertEqual()
• assertTrue() / assertFalse()
• assertRaises()

1. assertEqual()
This statement is employed to ascertain if the result obtained is adequate the expected result.

2. assertTrue() / assertFalse()
This statement is employed to verify if a given statement is true or false.

3. assertRaises()
This statement is employed to boost a selected exception.

Description of tests :

• test_strings_a
• test_upper
• test_isupper
• test_strip
• test_split

1. test_strings_a
This check is employed to check the property of string during which a personality say ‘a’ increased by variety say ‘x’ offers the output as x times ‘a’. The assertEqual() statement returns true during this case if the result matches the given output.

2. test_upper
This check is employed to ascertain if the given string is born-again to capital or not. The assertEqual() statement returns true if the string came back is in capital.

3. test_isupper
This check is employed to check the property of string that returns TRUE if the string is in capital else returns False. The assertTrue() / assertFalse() statement is employed for this verification.

4. test_strip
This check is employed to ascertain if all chars passed within the perform are stripped from the string. The assertEqual() statement returns true if the string is stripped and matches the given output.

5. test_split
This check is employed to ascertain the split perform of the string that splits the string through the argument passed within the perform and returns the result as list. The assertEqual() statement returns true during this case if the result matches the given output.

unittest.main() provides a command-line interface to the check script.On running the on top of script from the command, following output is created :

…..
— — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
Ran five tests in zero.000s
OK

Programming Example

import unittest
class Example(unittest.TestCase):
def setUp(self):
print(“Setup”)
def test1(self):
print(“test1”)
def test2(self):
print(“test2”)
def tearDown(self):
print(“TearDown”)
def test3(self):
print(“test3”)
def test_even(self):
n=int(input(“Enter a value: “))
res=n%2
self.assertEqual(res,0)
def test_range(self):
for i in range(0,6):
self.assertEqual(1%2,0)
def test_range(self):
for i in range(0,6):
with self.subTest(i==i):
self.assertEqual(i%2,0)
n=int(input(“Enter a number”))
m = int(input(“Enter a number”))
@unittest.skipIf(m==0,”One range is zero”)
def test_div(self):
self.assertFalse((self.n/self.m)>0)
@unittest.skip
def test_div2(self):
self.assertTrue((self.n / self.m) == 0)
@unittest.expectedFailure
def test_div2(self):
self.assertTrue((self.n / self.m) == 0)
if __name__==’__main__’:
unittest.main()

--

--