- Go to https://github.com/flatironinstitute/sciware-testing-python
- Log in to Github (if necessary)
- Click "Fork" in upper right
- Choose your username (if prompted)
git clone https://github.com/YOURUSER/sciware-testing-python.git- Other instructions under green "Code" button dropdown
cd sciware-testing-pythonpip3 install -e .- Or
pip3 install --user -e .without venv/conda
- Or
pytest
You should see: 12 passed, 2 skipped, 2 xfailed, 1 xpassed
- Code lives in
sciware_testing_python/*.py(main module:exercise.py) - Tests live in
tests/test_*.py - Let's look at
sciware_testing_python/exercise.pynowsum_numbers: what does it do? how do you use it?
- Open
tests/test_exercise.py - Write some tests for
sum_numbers- Fill in
test_sum_numbers_yoursandtest_sum_numbers_empty - Change
passtoassert sum_numbers(..) == ..
- Fill in
- Run
pytest tests/test_exercise.py
- Open
sciware_testing_python/exercise.py - Add another doctest to the lines in quoted documentation:
-
Example ------- >>> sum_numbers([]) 0 - Any
>>>line in a docstring will be executed and compared to the expected output on the following line
-
- Run
pytest --doctest-modules sciware_testing_python/exercise.py
- Look at
sciware_testing_python/exercise.pyadd_vectors- It has a problem (but don't fix it yet)
- Add a new test for
add_vectorstotests/test_exercise.py pytest tests/test_exercise.py: FAIL!- Fix
add_vectorsand try again- Hint: change
*to+
- Hint: change
- Update the
test_sum_stringstest intests/test_exercise.pyto callsum_numberson a list of strings pytest tests/test_exercise.py: FAIL- Tell pytest that this should actually fail:
- Add (uncomment)
@pytest.mark.xfail(strict=True)before the function
- Add (uncomment)
- Run tests again: now it should pass
- Open
tests/test_tdd.pyand look at the tests- We wrote a test before writing the code for
count_twos
- We wrote a test before writing the code for
- Remove the
@pytest.mark.skipline fortest_count_twos - Run
pytest tests/test_tdd.py: FAIL!count_twosdoesn't exist yet!
- Add a
count_twosfunction tosciware_testing_python/exercise.py - Run the tests again to pass
- See examples in
tests/test_examples.py @pytest.mark.parameterize- can be used to loop over input arguments for your test
@pytest.mark.parameterize("arg1, arg2, ...", [[val1, val2, ...], ...]
@pytest.fixture- Fixtures are called automatically to generate/load matching arguments
- Click on "Actions" tab
- Click "I understand ... enable"
- Open
.github/workflows/exercise.yml - Add
3.9topython-versionto add testing for Python 3.9 - Add a
pytest --doctest-modulesto the Test step - Commit and push (to your repo)
- Open
README.md - Fix the badge link to point to your repo
- Run
pytest --cov - Open
.github/workflows/exercise.yml - Update the Test step so it runs
pytest --doctest-modules --cov
- Setup your account at codecov.io
- Link it to
sciware-testing-python - Go to Settings -> Repository Upload Token and copy the token
- Link it to
- Uncomment the Codecov step and paste your token
- Commit and push (to your repo)
- Run
mypy - Add type some annotations to
sciware_testing_python/exercise.py(seeexamples.py)