05 Team Activity: Testing and Fixing Functions
Instructions
Work as a team as explained in the instructions for the lesson 2 team activity.
Purpose
Writing and running test functions often help a software
developer find mistakes in code. During this assignment, you will
write three test functions. Then use pytest
to run the
test functions and use the output of pytest
to help you
find and fix errors in some program functions.
Problem Statement
Most people around the world today have at least two names, a family name and a given name. In the United States, we usually write a person’s given name followed by his family name. However, when a computer lists names in alphabetical order, it is convenient to list the family name first and then the given name like this:
- Toussaint; Marie
- Toussaint; Olivier
- Washington; George
- Washington; Martha
When writing a program that alphabetizes names, it is often helpful to have the following three functions.
make_full_name
- Combines a person’s given name and family name into one string with the family name first
extract_family_name
- Extracts a person’s family name from his full name
extract_given_name
- Extracts a person’s given name from his full name
A programmer has already written those three functions. However, there are mistakes in at least two of the three functions.
Assignment
As a team, write three test functions named
test_make_full_name
,
test_extract_family_name
, and
test_extract_given_name
. Each of the test functions
will test one of three previously written program functions. Use
pytest
to run the test functions and find and fix the
mistakes, if any, that are in program functions.
Helpful Documentation
- The prepare content for this lesson explains how to use
pytest
,assert
, andapprox
to automatically verify that functions are correct. It also contains an example test function and links to additional documentation aboutpytest
. - This video about test functions (20 minutes) shows a BYU-Idaho faculty member writing two test functions and using
pytest
to run them.
Steps
Do the following:
- Download the names.py Python file and save it in the same folder where you will save your Python test program. Then open the downloaded file in VS Code. Notice that the downloaded file contains three small functions named:
make_full_name
,extract_family_name
, andextract_given_name
. Notice also that each function has a documentation string (a triple quoted string immediately below the function header) that describes what the function does. Read the documentation strings for all three functions. The code in the functions may contain some mistakes. - Using VS Code, open a new Python file and copy and paste the following import statements at the top of the file.Save the file with the name
from names import make_full_name, \ extract_family_name, extract_given_name import pytest
test_names.py
in the same folder where you downloaded and saved thenames.py
file. - In
test_names.py
, write a function namedtest_make_full_name
that takes no parameters. Writeassert
statements inside this function to test the value returned from themake_full_name
function. If you are not sure what themake_full_name
function does or how to test it, read the documentation string that is at the top of themake_full_name
function in thenames.py
file. - In
test_names.py
write a function namedtest_extract_family_name
that takes no parameters. Writeassert
statements inside this function to test the value returned from theextract_family_name
function. - In
test_names.py
write a function namedtest_extract_given_name
that takes no parameters. Writeassert
statements inside this function to test the value returned from theextract_given_name
function. - At the bottom of your
test_names.py
file, write a call to thepytest.main
function like this:# Call the main function that is part of pytest so that the # computer will execute the test functions in this file. pytest.main(["-v", "--tb=line", "-rN", __file__])
- Save your
test_names.py
file and run it by clicking the green run icon in VS Code. - Read the output from
pytest
and use the output to help you find and fix any errors that are in your test functions or themake_full_name
,extract_family_name
, andextract_given_name
functions. - Repeat steps 7 and 8 until you have found and fixed all the mistakes and your three test functions pass.
Core Requirements
- Write
test_make_full_name
so that it testsmake_full_name
with various names, including long names, short names, and hyphenated names. Fix the mistake in themake_full_name
function. - Write
test_extract_family_name
so that it testsextract_family_name
with various names, including long names, short names, and hyphenated names. - Write
test_extract_given_name
so that it testsextract_given_name
with various names, including long names, short names, and hyphenated names. Fix the mistake in theextract_given_name
function.
Stretch Challenges
If your team finishes the core requirements in less than an hour, complete one or more of these stretch challenges. Note that the stretch challenges are optional.
In the United States, mailing addresses are supposed to be written
in this form: number street, city, state zipcode
For example: 525 S Center St, Rexburg, ID 83460Download and save this Python file named address.py. Open a new Python file named
test_address.py
and write a test function namedtest_extract_city
that verifies that theextract_city
function works correctly.- Write a test function named
test_extract_state
that verifies that the extract_state function works correctly. - Write a test function named
test_extract_zipcode
that verifies that the extract_zipcode function works correctly.
Testing Procedure
Before you fix the mistakes in the make_full_name
and extract_given_name
functions, pytest
will print output similar to this:
> python test_names.py ===================== test session starts ====================== platform win32--Python 3.8.6, pytest-6.1.2, py-1.9.0, pluggy-0.1 rootdir: C:\Users\cse111\lesson05 collected 3 items test_names.py::test_make_full_name FAILED [ 33%] test_names.py::test_extract_family_name PASSED [ 66%] test_names.py::test_extract_given_name FAILED [100%] ========================== FAILURES ============================ C:\Users\cse111\early-functions\docs\lesson05\teach_solution.py: AssertionError: assert 'Smith-Jones;Ava' == 'Smith-Jones; Ava' C:\Users\cse111\early-functions\docs\lesson05\names.py:31: ValueError: substring not found ================= 2 failed, 1 passed in 0.14s ==================
After you fix the mistakes in the make_full_name
and
extract_given_name
functions, pytest
will
print output similar to this:
> python test_names.py ===================== test session starts ====================== platform win32--Python 3.8.6, pytest-6.1.2, py-1.9.0, pluggy-0.1 rootdir: C:\Users\cse111\lesson05 collected 3 items test_names.py::test_make_full_name PASSED [ 33%] test_names.py::test_extract_family_name PASSED [ 66%] test_names.py::test_extract_given_name PASSED [100%] ====================== 3 passed in 0.12s =======================
Sample Solution
Please work diligently with your team for the one hour meeting. After the meeting is over, please compare your solution to the sample solution and the stretch solution. Please do not look at the sample solutions until you have either finished the program or diligently worked for at least one hour. At the end of the hour, if you are still struggling to complete the assignment, you may use the sample solution to help you finish.
Call Graph
The following call graph shows the function calls and returns in
the sample solution for this assignment. From this call graph we see
that the computer starts executing the sample test functions by
calling the pytest.main
function. While executing the
pytest.main
function, the computer calls the
test_make_full_name
function which calls the
make_full_name
function. Then while still executing
pytest.main
, the computer calls the
test_extract_family_name
function which calls the
extract_family_name
function. Then while still
executing pytest.main
, the computer calls the
test_extract_given_name
function which calls the
extract_given_name
function.
Ponder
During this assignment, you downloaded three program functions
that work with a person’s name. You wrote three test functions that
each test one of the program functions. You used pytest
to run your test functions and used the output of
pytest
to help you find and fix the mistakes in the
program functions. How will writing and running test functions help
you write better programs?
Submission
When you have finished the activity, please report your progress via the associated I‑Learn quiz. When asked about which of the requirements you completed, feel free to include any work done during the team meeting or after the meeting, including work done with the help of the sample solution, if necessary. In short, report on what you were able to accomplish, regardless of when you completed it or if you needed help from the sample solution.