From 3d9e9f35455dd127f587393ed60128b88a897d7d Mon Sep 17 00:00:00 2001 From: Ryan May Date: Thu, 9 Nov 2017 23:06:26 -0600 Subject: [PATCH] Add unit on testing --- environment.yml | 1 + presentations/Testing/Testing.md | 52 +++++++++++++++++++ presentations/Testing/stats.py | 13 +++++ presentations/Testing/temperature.py | 8 +++ presentations/Testing/tests/test_stats.py | 3 ++ .../Testing/tests/test_temperature.py | 3 ++ 6 files changed, 80 insertions(+) create mode 100644 presentations/Testing/Testing.md create mode 100644 presentations/Testing/stats.py create mode 100644 presentations/Testing/temperature.py create mode 100644 presentations/Testing/tests/test_stats.py create mode 100644 presentations/Testing/tests/test_temperature.py diff --git a/environment.yml b/environment.yml index 22da1a52..b5472ab9 100644 --- a/environment.yml +++ b/environment.yml @@ -16,6 +16,7 @@ - xarray - pip - ffmpeg + - pytest - pip: - python-awips - mpldatacursor diff --git a/presentations/Testing/Testing.md b/presentations/Testing/Testing.md new file mode 100644 index 00000000..9cabe27a --- /dev/null +++ b/presentations/Testing/Testing.md @@ -0,0 +1,52 @@ +Testing +======= + +What is Software Testing? +------------------------- +- Testing is a way to make sure that software keeps working like you expect +- You write code that runs other code and validates the results + +Types of Tests +-------------- +- Unit tests + - Tests of individual units of code (functions, classes). + - Test wide array of inputs and cases try to exhaustively capture cases +- Regression tests + - Test that nothing has broken after making changes +- Integration tests + - Test that different pieces of code work together properly +- Acceptance tests + - Test that code meets certain sets of external requirements + +- None of that matters to us as scientists + +Testing Strategies +------------------ +- The right kind of test for your code: the kind you're willing to write +- Requirements: + - Automated: computer can evaluate whether the test passes + - Consistent: no spurious failures + - Self-contained: no external resources, the test completely controls everything +- At the very least, when you write code, you run it and verify the results + - Why not turn this into an automated test + - Doesn't prove correct + - Verifies that results you thought were sufficient haven't changed +- For some code, can use e.g. values from source reference +- In an ideal world, you write several tests spanning: + - input types (e.g. arrays, scalars) + - input domains + - edge cases + +Interactive: + - Add tests for `temperature.py` to `tests/test_temperature.py` + - values + - round trip + - How do we run tests: `pytest + +Exercise: + - Add calculations for mean and median to `stats.py` + - Add tests to make sure everything is working fine + - Make sure it works for both even- and odd-sized arrays + +Exercise: + - Use tests to find the bugs in the `moving_average` function diff --git a/presentations/Testing/stats.py b/presentations/Testing/stats.py new file mode 100644 index 00000000..9c850390 --- /dev/null +++ b/presentations/Testing/stats.py @@ -0,0 +1,13 @@ +import numpy as np + +def median(): + pass + +def mean(): + pass + +def moving_average(vals, n): + out = [] + for i in range(len(val) - n - 1): + out[i] = vals[i:i + n].mean() + return np.arry(vals) diff --git a/presentations/Testing/temperature.py b/presentations/Testing/temperature.py new file mode 100644 index 00000000..f6fee241 --- /dev/null +++ b/presentations/Testing/temperature.py @@ -0,0 +1,8 @@ +def to_kelvin(celsius): + return celsius + 273.15 + +def to_celsius(fahrenheit): + return (fahrenheit - 32) / 1.8 + +def to_fahrenheit(celsius): + return 1.8 * celsius + 32 diff --git a/presentations/Testing/tests/test_stats.py b/presentations/Testing/tests/test_stats.py new file mode 100644 index 00000000..aa5136ff --- /dev/null +++ b/presentations/Testing/tests/test_stats.py @@ -0,0 +1,3 @@ +# +# ADD TESTS HERE +# diff --git a/presentations/Testing/tests/test_temperature.py b/presentations/Testing/tests/test_temperature.py new file mode 100644 index 00000000..aa5136ff --- /dev/null +++ b/presentations/Testing/tests/test_temperature.py @@ -0,0 +1,3 @@ +# +# ADD TESTS HERE +#