Unit Test - Md-Ashraful-Alam/CSE327_Section05_Group03_Hospital_Management_System GitHub Wiki
Unit Test
unittest — Unit testing framework
Overview
The unittest unit testing framework was originally inspired by JUnit and has a similar flavor as major unit testing frameworks in other languages. It supports test automation, sharing of setup and shutdown code for tests, aggregation of tests into collections, and independence of the tests from the reporting framework.
To achieve this, unittest supports some important concepts in an object-oriented way:
Test Fixture
A test fixture represents the preparation needed to perform one or more tests, and any associated cleanup actions. This may involve, for example, creating temporary or proxy databases, directories, or starting a server as a process.
Test Case
A test case is the individual unit of testing. It checks for a specific response to a particular set of inputs. It also unittest provides a base class, Test-Case, which may be used to create new test cases.
Test Suite
A test suite is a collection of test cases, test suites, or both. It is used to aggregate tests that should be executed together.
Test Runner
A test runner is a component that orchestrates the execution of tests and provides the outcome to the user. The runner may use a graphical interface, a textual interface, or return a special value to indicate the results of executing the tests.
Basic Example
import unittest
class TestStringMethods(unittest.TestCase):
def test_upper(self):
self.assertEqual('cse'.upper(), 'CSE')
def test_isupper(self):
self.assertTrue('CSE'.isupper())
self.assertFalse('Fazle Rabby'.isupper())
def test_split(self):
str = 'Sofrware Engineering'
self.assertEqual(str.split(), ['Sofrware', 'Engineering'])
# check that str.split fails when the separator is not a string
with self.assertRaises(TypeError):
str.split(2)
if __name__ == '__main__':
unittest.main()
Usage
- Import unittest
- Test name must start with test_
- From terminal run command: python file_name.py
Testing
def area_of_Circle(radius):
if radius < 1:
raise ValueError('Radius must be greater than ZERO!')
return 3.1416 * (radius)^2
def perimeter_of_Circle(radius):
if radius < 1:
raise ValueError('Radius must be greater than ZERO!')
return 2 * 3.1416* radius
Test Class
import unittest
import Circle
class TestCircle(unittest.TestCase):
def test_area_of_Circle(self):
self.assertEqual(Circle.area_of_circle(5), 78.54)
self.assertEqual(Circle.area_of_circle(6), 113.09)
def test_perimeter_of_circle(self):
self.assertEqual(Circle.perimeter_of_circle(5), 31.42)
self.assertEqual(Circle.perimeter_of_circle(6), 37.69)
if __name__ == '__main__':
unittest.main()