Skip to content

Learning Python: Day 36

Chapter 11 – Testing Your Code

  • learn to test code using pytest
  • pytest library – a collection of tools that will help you write your first tests quickly, while supporting your tests as they grow in complexity
  • will learn how to install external libraries, which will make a variety of well-designed code available to you, like pytest
  • learn to build a series of tests and check that each set of inputs results in the output you want
  • see what a passing test and a failing test look like, and learn how the latter can improve your code
  • learn to test functions and classes

Installing pytest with pip

  • third-party package – a library that’s developed outside the core Python language
  • some packages are incorporated into the standard library
  • many packages are kept out of the standard library so they can be developed on a timeline independent of the language itself

Updating pip

  • pip – a tool that installs third-party packages
  • need to update it often for security

$ python -m pip install –upgrade pip

Requirement already satisfied: pip in /…/python3.11/site-packages

(22.0.4)

–snip–

Successfully installed pip-22.1.2

  • command breakdown
  • python -m pip tells Python to run the module pip
  • install –upgrade tells pop to update a package that’s already been installed
  • pip specifies which third-party package should be updated
  • use this command to update any third-party package installed on your system

$ python -m pip install –upgrade package_name

Installing pytest

$ python -m pip install –user pytest

Collecting pytest

–snip–

Successfully installed attrs-21.4.0 iniconfig-1.1.1 …pytest-7.x.x

  • still using the core command pip install without the –upgrade flag this time
  • we’re using –user flag instead, which tells Python to install this package for the current user only
  • output shows latest version of pytest was successfully installed along with a number of other packages that pytest depends on
  • can use this command to install many third-party packages

$ python -m pip install –user package_name

Testing a function

  • need to test code
  • here’s a function that takes in a first and last name, and returns a neatly formatted full name

name_function.py

def get_formatted_name(first, last):

….”””Generate a neatly formatted full name.”””

….full_name = f”{first} {last}”

….return full_name.title()

  • function get_formatted_name() combines the first and last name with a space in between to complete a full name, and then capitalizes and returns the full name
  • check that get_formatted_name() works, let’s make a program that uses this function
  • program names.py lets users enter a first and last name

names.py

from naem_function import get_formatted_name

.

print(“Enter ‘q’ at any time to quit.”)

while True:

….first = input(“\nPlease give me a first name: “)

….if first == ‘q’:

……..break

….last = input(“Please give me a last name: “_

….if last == ‘q’:

……..break

.

….formatted_name = get_formatted_name(first, last)

….print(f”\tNeaetly formatted name: {formatted_name}.”)

  • program imports get_formatted_name() from name_function.py
  • user can enter a series of first and last names and see the formatted full names that are generated

Enter ‘q’ at any time to quit.

.

Please give me a first name: janis

Please give me a last name: joplin

……..Neatly formatted name: Janis Joplin.

.

Please give me a first name: bob

Please give me a last name: dylan

……..Neatly formatted name: Bob Dylan.

.

Please give me a first name: q

  • names generated correctly
  • can modify get_formatted_name() so it can handle middle names
  • don’t want to break the function
  • pytest can automate the testing of a function’s output

End of study session.

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *