W03 Code-along Activity: Testing and Fixing Functions
Code-along Activity Overview
For this activity, you will code along with an instructor video that walks you through each step of the activity below. Make sure that you pause the video, or watch sections over if you need to, so that you are able to complete the entire activity.
Your instructor will post a link to the video in Microsoft Teams.
Background
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.
Program
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.
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.
Enhancements
Here is a list of enhancements that you could make to the program. Your instructor will walk you through at least one of them. Feel free to complete others.
-
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.
Instructions
- Create a folder for this week's code-along program.
- Download the names.py python file and save it in your project folder you just created.
- Open the folder you just created in VSCode.
- Find the video that your instructor has posted for this assignment and code along with them to complete each of the requirements. Work through the requirements in order with the instructor rather than jumping ahead to more complicated steps to ensure you do not miss any concepts.
Alternative Video Link and Transcript
In addition to the link that your instructor posted, you are also welcome to watch the following code walkthrough or use its transcript:
Additional Resources
You may use these additional resources for further study.
Sample Solution
In addition to the instructor video, you can also view this solution. (This solution may be slightly different than the final code that you and your instructor created.)
Helpful Documentation
This video about test functions (20 minutes) shows a BYU-Idaho faculty member writing two test functions and usingpytest
to run them.
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\week03 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\week03\teach_solution.py: AssertionError: assert 'Smith-Jones;Ava' == 'Smith-Jones; Ava' C:\Users\cse111\early-functions\docs\week03\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\week03 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 =======================
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.
Submission
Please pause and re-watch any sections of the instructor video necessary until you have completed the entire activity and your program runs the same as the instructor's. When you are finished:
- Return to Canvas to take the quiz.
Useful Links:
- Return to: Week Overview | Course Home