PyUnit Example
You will create and run your first PyUnit test using an IDE in this lesson. Let’s run a sample PyUnit test using Eclipse IDE. Follow the below steps:
Create Python Test
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. Â
Â