This repository is part of the class activity for Intro to Tests in Unit 1.
- Practice identifying test cases
- Practice writing tests with pytest
Follow these directions once when you start working on this activity during roundtables:
- Navigate to your projects folder named
projects
$ cd ~/Developer/projects
- In Github click on the green "Code" button in Github and then copy the URL. This will allow you to download a copy of this project into your projects folder.
After copying the URL, run the following command in your terminal:
$ git clone <paste your copied URL here>
This command makes a new folder called intro-to-tests-activity
, and then puts the project into this new folder. Use ls
to confirm there's a new project folder
- Move your location into this project folder
$ cd intro-to-tests-activity
- Create a virtual environment named
venv
for this project:
$ python3 -m venv venv
- Activate this environment:
$ source venv/bin/activate
- Verify that you're in a python3 virtual environment by running:
$ python --version
should output a Python 3 version$ pip --version
should output that it is working with Python 3
- Install dependencies once at the beginning of this project while your virtual environment is activated. Note that
(venv)
is not part of the command below that you need to run.
$ (venv) pip install -r requirements.txt
We will be writing tests for some pre-written code. Once everyone has the repo cloned and ready to go, follow the steps below together.
- Look through the directory structure to familiarize yourself with how the files are organized.
- Look at the feature provided in
main.py
and discuss as a group what it is meant to be doing. - As a group, come up with a few nominal and edge cases you would like to test relating to the feature provided.
- Find the appropriate file in which to write your tests and start writing them.
NOTE There are currently two prewritten dummy tests used for demonstration purposes. Feel free to delete these once you have written your own tests.
-
Find the test file that contains the test you want to run.
- Check the
tests
folder, and find the test file you want to run - In that test file, read through each test case
- Check the
-
Run the tests for that specific project
# Must be in activated virtual environment
$ pytest tests/test_count_a_letter.py
- Run all tests that exist in this project with:
# Must be in activated virtual environment
$ pytest
- If you want to see any
print
statements print to the console, add-s
to the end of anypytest
command:
# Must be in activated virtual environment
$ pytest -s
- With pytest installed, you should see a beaker icon in the toolbar on the left. Click the icon and you should be prompted with an opportunity to configure test. Click the button to configure tests.
- A selector will pop up at the top of your VS Code asking which testing framework you would like to use. By default, unittest will be selected. Make sure you select pytest
- The selector will then ask which directory our tests are in. As they are currently in the
tests
directory, selecttests
.
- If done correctly, you should now see your tests pop up in the sidebar, similar to what is below:
- To run tests all at once, press the play button at the top of the test exploration tab. To run a single test, open up the test tree, hover over the test you want to run and press the play button.