PythonPlacement Classroomnotes 21/Feb/2022

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
    Preview
  • The installations of packages which we have done in our project have to be explicitly defined.
    Preview
  • 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
    Preview
  • Now lets write a simple test module test_sample with test case test_add_numbers. Lets see the result
    Preview
  • If we have some error the output will be as shown below
    Preview
  • Lets configure pytest in vscode
    Preview
    Preview
    Preview
    Preview
  • Now lets debug
    Preview
  • Refer Here for the changes done
    Preview

Exercise: Write a function and test cases in a new module

  • This module can be called as picnic and test module will be test_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

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About continuous learner

devops & cloud enthusiastic learner