PyUnit Example
You will create and run your first PyUnit test using an IDE in this lesson. Before writing unit tests, it’s important to understand the following items.
Test fixture: A Test fixture expresses the initial conditions and process for executing the test, followed by clean-up actions.
Test case: The test case is the basic unit of testing. It specifies test conditions, actions, and expected results for a specific software application’s functionality.
Test suite: A test suite is a collection of test cases.
Test runner: A test runner is a software tool or framework that executes tests and reports the results.
Create Python Test
Let’s run a sample PyUnit test using Eclipse IDE. Follow the below steps:
Launch Eclipse IDE.
Create a new PyUnit test File >> New >> File
Give a name to the test.
The filename should have a .py extension.
For example, myfirsttest.py
Add the following code:
# Python Unit Test Example
# Python Tutorials - www.TestingDocs.com
import unittest
class MyTestCase(unittest.TestCase):
def test_example(self):
self.assertEqual(7 + 9, 16)
if __name__ == '__main__':
unittest.main()
Â
Run Python Test
Save the code. Choose the following menu option to save the test.
File >> Save The keyboard shortcut is the Ctrl + S key combination.
Run the test.
Right-click on the test and choose
Run As >> Python unit-test option. Â
Â
Â
Test Output
The graphical output console is the PyUnit Console, which displays green or red if the test is passed or failed.
The following output should be displayed in the console:
Ran 1 test in 0.000s
OK
This means that one test has run and passed.
The output will also display the time the test(s) took to run. Â
Â