CH2 Testcase Python
CH2 Testcase Python
Python
Python –ch2
two key methods of testing your
Python code: doctest and pytest.
Writing tests ensures that your functions behave as expected, and
catching errors early makes your code more robust.
• 1. Doctests: Basic Testing in Docstrings
• A doctest is a quick way to test your code by embedding examples
directly in the function's docstring. These examples demonstrate how
the function should behave and can be run automatically to check if
your code is working as
2. Creating Test
Suites with Pytest
• While doctests are useful for basic examples
• pytest allows more extensive and separate testing of your functions.
• It supports creating test files and using assertions to validate the
behavior of your code.
• Each function starting with test_ is considered a test case.
Steps to Use Pytest:
Pytest will automatically discover and run all the tests in files
that follow the naming convention test_*.py.
• Using PyCharm's Run/Debug Configuration
1.Right-click on the test file or the directory containing test files.
2.From the context menu, choose Run 'pytest in <folder_name>'.
https://www.teach.cs.toronto.edu/~csc148h/notes/testing/how_to_test.html