Unit Test Activity
1. Get Situated / Pull Down the Files
On GitHub: sync your fork of the class-exercises-spring2025 repo on GitHub.
On your local machine:
- Navigate to your
csci338/class-exercises-spring2025directory - Check that everything is committed and pushed to your remote branch (some command reminders below):
git branch git status git add . git commit -m "Message describing your changes" - Checkout
main - Pull down the latest code (
git pull). You should see a newwriting-testsfolder within your localclass-exercises-spring2025folder. - Create a new branch from main called writing-tests-b:
git checkout -b writing-tests-b
2. Configure your development environment using Poetry
- Navigate into your
writing-testsdirectory from the command line. - Open the entire
writing-testsdirectory in VS Code by typinccode .on the terminal. - Create a new Poetry project:
poetry init(take all the defaults by pressing the enter key). - Install the following dependencies with
poetry add
pytest black flake8 isort
3. Run the python files and the tests:
Navigate back into the writing-tests directory on the command line. Then type the following:
poetry run python app/main.py # runs the app
poetry run pytest -v # runs the tests
Now, open tests/test_main.py and take a look at it. Uncomment the tests one by one.
- What do you think the decorators do?
- Under what circumstances might it be important to create test mocks?
4. Refactor the code
Organize code for doing math
- You decided to create a new class that contains static methods for doing Arithmetic (I have already created this class for you – just uncomment the first static method I have created for you).
- Delete the
add_numsfunction defintion frommain.pyand use the one inArithmeticinstead. - Run
test_main.pyagain. Fix the failing tests.
Organize code for interacting with the user
- Create a new Python class called
UserInterface. (I have already created a stub for this class. You will need to migrate the functions and convert them into static methods (by using the@staticmethoddecorator. - Delete
add_nums_from_user_inputfunction defintion frommain.pyand use the one inUserInterfaceinstead. - Fix your tests so that they work again.
Metacomment about testing:
- One of the most useful things about tests is that they will tell you when your code refactor broke something.
- One of the challenges of tests (as you’ve already seen) is that you have to maintain them. Figuring out ways of designing manageable tests is a lifelong pursuit.
5. Create two new static methods
- Create a new static method in the Arithmetic class called
subtract_numsthat subtracts two numbers. - Create a new static method in the Arithmetic class called
subtract_nums_from_user_inputthat subtracts two numbers based on user input. - Write some tests to test your new methods.
6. Use the static analysis / formatter tools
- Run the import sorter check:
poetry run isort . --check-only - Run the import sorter fix:
poetry run isort - Run the formatter check:
poetry run black . - Run the formatter fixes:
poetry run black . - Run flake8 (linter / code quality checker):
poetry run flake8