Assignment 2 - Unit testing

Practice implementing and testing simple functions.

Objective

The goal of this assignment is to gain more practice with the tools we have used so far, including implementing a unit test with pytest.

Setup

  1. Create a new project directory named assn2/ or something similar.
    • Mac and Ubuntu on VirtualBox users: Open that directory using PyCharm as usual.
    • WSL users: Follow the setup process as the bottom of the page.
  2. Download grades.py to the assn2/ folder

Instructions

The assignment is to be completed alone. Refer to the Syllabus for policies on what is allowed and what is not.

Implementation

  1. Put your name at the top.
  2. You may not change the main() function in any way.
  3. You may add to the __main__ block to help test if you want.
  4. Complete the functions calculate_average() and determine_grade() according to their docstring descriptions:
    • The docstring tells you what the function must do, the parameters passed to it, the type that must be returned, and any exceptions you need to raise.
    • You may not add any parameters, change the return type, or add to or alter the exceptions required.
    • You must not call print() or input() from these functions.

Testing

  1. Create a test file for grades.py.
  2. Put your name at the top of your test file in a comment.
  3. Write one or more test cases in your test file for calculate_average().
    • The test case must invoke calculate_average() by providing a list, e.g., calculate_average([1,2,6]). Use assert statements to ensure the computed value is correct. You should have multiple asserts to check the calculation.
    • You must write test cases that check the exceptional conditions that raise value errors. Refer to the lab on testing for exceptions.
  4. Write one or more test cases in your test file for determine_grade(). This function does not knowingly raise exceptions, so you do not need to test for them. Test only for expected output given an input.
  5. Run your test file using pytest.

Rubric

  • (15 pts): Your implementation of calculate_average() passes my test cases, which exercises all the details of the function’s docstring.
  • (10 pts): Your implementation of determine_grade() passes my test cases, which exercises all the details of the function’s docstring.
  • (15 pts): Create a control flow graph (CFG) for each of your implementations of calculate_average() and determine_grade() following the rules from class.
    • You may use an online flow chart tool such as draw.io, Canva, or Lucidchart.
    • You may draw your CFG on paper and take a picture. Ensure it is legible.
    • In both cases, export your CVG image to a PDF to submit to Canvas.
  • (25 pts): Your test file can be run using pytest, has multiple test cases, and thoroughly tests the parameters, returns, computations, and exceptions raised of the functions as specified by their docstrings.
  • Your assignment will receive a score of 0 pts for any of the following:
    • print() or input() statements in calculate_average() or determine_grade()
    • Changing main() in any way
    • Changing the method signature of calculate_average() or determine_grade()
    • Your code or tests fail due to syntax error.

Submission due Feb 23

Submit your grades.py, your test file, and your CFG PDF file to the Canvas assignment page.

Setup for WSL Users

You will follow this process for every project on WSL from now on.

  1. Open PyCharm and select the File menu, then Close Project.
  2. Select WSL on the left, then the + button to create a new project.
  3. Select the ... button to pick the Project directory.
  4. Pick your Ubuntu instance at the top, then navigate to home/<your_id>/seng-201/ and create a new folder (icon at the top) for assn2/.
  5. Select the new directory and hit OK.
  6. Click Start IDE and Connect on the screen. PyCharm will take a minute to finish configuring. It should open a new window with a main.py file showing some boilerplate code.
  7. Select the File menu, then Settings.
  8. Select Project: assn2 in the left pane, then click the Python Interpreter link.
  9. Select the Add Interpreter link near the top right, then Add Local Interpreter.
  10. Leave the default options selected and hit OK. If you see a red error message, contact the instructor.
  11. OK out of the settings screen.
  12. Finally, open a new Terminal within PyCharm. Type which pip. You should see something like
    • /home/<your_id>/seng-201/assn2/.venv/bin/pip, or;
    • /home/<your_id>/virtualenvs/assn2/bin/pip
    • but not /usr/bin/python
  13. You will run all subsequent Terminal commands from the integrated Terminal in PyCharm.
  14. run the following in the integrated Terminal:
    pip install pytest pytest-cov
    
  15. Complete the setup instructions at the top of this lab.
Last modified February 22, 2025.