Unit Testing/Pytest/Create a unittest Test
Appearance
< Unit Testing | Pytest
Create a Python Program
[edit | edit source]Create the following Python program. Save it as multiply.py
def multiply(x, y):
result = x * y
return result
Create a Test Program
[edit | edit source]Create the following Pytest test program. Save it as either test_multiply.py
or multiply_test.py
import unittest
from multiply import *
class MultiplyTestCase(unittest.TestCase):
def test_multiply(self):
self.assertEqual(
multiply(2, 2), 4, "Error: 2 * 2 = 4"
)
if __name__ == '__main__':
unittest.main(exit=False)
Test Success
[edit | edit source]Test the program by running pytest in a terminal or command prompt in the same folder as the two programs above and observe the results.
pytest
Test Failure
[edit | edit source]Change the multiply source code somehow, such as multiplying by 0 rather than multiplying by y. Test the program by running pytest again and observe the results.