0% found this document useful (0 votes)
4 views

CH2 Testcase Python

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

CH2 Testcase Python

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Testing Your Work in

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:

• Make sure pytest is installed


If you're using PyCharm, you can install pytest directly from the IDE:
1.Go to File > Settings (on macOS: PyCharm > Preferences).
2.Navigate to Project > Python Interpreter.
3.Click the + button to add a new package.
4.Search for pytest in the dialog box.
5.Select pytest and click Install Package.
2. Write your tests in a separate file (e.g., test_functions.py):

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

You might also like