CSCI 338: Spring 2025

Software Engineering

CSCI 338: Spring 2025

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:

2. Configure your development environment using Poetry

  1. Navigate into your writing-tests directory from the command line.
  2. Open the entire writing-tests directory in VS Code by typinc code . on the terminal.
  3. Create a new Poetry project: poetry init (take all the defaults by pressing the enter key).
  4. 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.

4. Refactor the code

Organize code for doing math

  1. 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).
  2. Delete the add_nums function defintion from main.py and use the one in Arithmetic instead.
  3. Run test_main.py again. Fix the failing tests.

Organize code for interacting with the user

  1. 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 @staticmethod decorator.
  2. Delete add_nums_from_user_input function defintion from main.py and use the one in UserInterface instead.
  3. Fix your tests so that they work again.

Metacomment about testing:

  1. One of the most useful things about tests is that they will tell you when your code refactor broke something.
  2. 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

  1. Create a new static method in the Arithmetic class called subtract_nums that subtracts two numbers.
  2. Create a new static method in the Arithmetic class called subtract_nums_from_user_input that subtracts two numbers based on user input.
  3. Write some tests to test your new methods.

6. Use the static analysis / formatter tools

  1. Run the import sorter check: poetry run isort . --check-only
  2. Run the import sorter fix: poetry run isort
  3. Run the formatter check: poetry run black .
  4. Run the formatter fixes: poetry run black .
  5. Run flake8 (linter / code quality checker): poetry run flake8