Skip to content
This repository has been archived by the owner on Jan 9, 2020. It is now read-only.

Commit

Permalink
Add unit on testing
Browse files Browse the repository at this point in the history
  • Loading branch information
dopplershift committed Nov 10, 2017
1 parent df5bd3f commit 3d9e9f3
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 0 deletions.
1 change: 1 addition & 0 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- xarray
- pip
- ffmpeg
- pytest
- pip:
- python-awips
- mpldatacursor
52 changes: 52 additions & 0 deletions presentations/Testing/Testing.md
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions presentations/Testing/stats.py
Original file line number Diff line number Diff line change
@@ -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)
8 changes: 8 additions & 0 deletions presentations/Testing/temperature.py
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions presentations/Testing/tests/test_stats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#
# ADD TESTS HERE
#
3 changes: 3 additions & 0 deletions presentations/Testing/tests/test_temperature.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#
# ADD TESTS HERE
#

0 comments on commit 3d9e9f3

Please sign in to comment.