How else do we know it works?
Does it work?
Is it correct?
How can we prove it?
<Figure size 960x480 with 0 Axes>
<Figure size 960x480 with 0 Axes>
Testing Domains
(What the planes mean)
Testing Approaches
(How we measure residuals)
def test_raises_value_error_if_sparse():
error_msg = "dense data is required."
# X must not be sparse if positive == True
X = sparse.eye(10)
y = np.ones(10)
reg = LinearRegression(positive=True)
with pytest.raises(TypeError, match=error_msg):
reg.fit(X, y)
Example modified from here
def test_pipeline_methods_anova():
X, Y = iris.data, iris.target
# Test with Anova + LogisticRegression
clf = LogisticRegression()
filter1 = SelectKBest(f_classif, k=2)
anova = ("anova", filter1)
logistic = ("logistic", clf)
pipe = Pipeline([anova, logistic])
pipe.fit(X, y)
pipe.predict(X)
pipe.predict_proba(X)
pipe.predict_log_proba(X)
pipe.score(X, y)
Example modified from here
def test_user_story_1():
# read data from disk
spool = dc.load('example_files')
# chunk data, filter
out = []
for patch in spool.chunk(time=10):
pa = patch.pass_filter(time=(None, 10))
out.append(pa)
# create new spool
proc_spool = dc.spool(out)
# plot first patch
proc_spool[0].viz.waterfall(show=True)
# save to disk
proc_spool.io.write('processed', 'DASDAE')
<Figure size 960x480 with 0 Axes>
<Figure size 960x480 with 0 Axes>
image source here
Original function and tests
Mutated function (tests unchanged)
<Figure size 960x480 with 0 Axes>
The law of diminishing returns applies!
Selecting the right level of testing
How to use pytest?
pytest
from the command line, it will then:pytest to test our add function:
Organizing tests
Organizing tests
Example: Write extra tests for numpy’s linalg norm
Run tests from the command line using pytest
.
What domain and approach does test_norm use?
Domain: Unit
Approach: Example
https://docs.pytest.org/en/6.2.x/fixture.html#what-fixtures-are
Scopes control how often fixtures are run: “function”, “class”, “module”, “package”, “session”
What’s wrong here?
import numpy as np
import pytest
@pytest.fixture()
def data():
return [1, 2, 3]
def test_data_1():
assert isinstance(data, list)
test_data_1 needs the data argument!
What’s wrong here? How can we fix it?
Run slow tests