Adding Testing Libraries to your virtual environment
-
We have add the basic code to add two numbers Refer Here
-
To test this code we would be using the following block with different
if __name__ == "__main__":
print(add(5,6))
print(add(-1,1))
- The better way of testing your code is by performing unit tests.
- Install pytest
pip install pytest
- The installations of packages which we have done in our project have to be explicitly defined.
- If the code is shared with other developer, the other developer has to create a venv and the activate the venv and execute the command
pip install -r requirements.txt
- Now lets write a simple test module
test_sample
with test casetest_add_numbers
. Lets see the result
- If we have some error the output will be as shown below
- Lets configure pytest in vscode
- Now lets debug
- Refer Here for the changes done
Exercise: Write a function and test cases in a new module
- This module can be called as
picnic
and test module will betest_picnic
- Create a function called as
food_items
which takes any number of arguments
Function Call Sample Output
food_items('salad') => You are bringing salad
food_items('salad', 'chips') => You are bringing salad and chips
food_items('salad', 'chips', 'cake') => You are bringing salad, chips and cupcakes
- Refer Here for the solution