comms: swc slack | github | email
Sainsbury Wellcome Centre pyClub. 2023-04-05.
https://scnlf.me/swc-pyclub-tdd
git@github.com:samcunliffe/swc-pyclub-tdd.git
โ
1996. Extreme programming (XP). 1999. Beck, K., Extreme Programming Explained (1st Ed) 2001. http://agilemanifesto.org/ โโ (Agile is "just" an approach to sw dev.) 2003. Beck, K., Test-Driven Development by Example (1st Ed) (O'Reilly via UCL)
"Rediscovery of TDD"
1957. McCracken, D. D., Digital Computer Programming.
"... it is highly desirable that the 'customer' prepare the check case ..."
You have a feature or spec. or a bug.
You are not allowed to write any new active implementation code unless it is to make an existing test pass.
git clone git@github.com:samcunliffe/swc-pyclub-tdd.git cd swc-pyclub-tdd pip install -e ".[dev]"
main
Trust the process.
(In no particular order.)
Do use TDD for bugfixing.
Do use TDD for clarifying specs.
Do use TDD for complicated/unfamiliar codebases.
Beware (anti-)implementation.
(A stupid example.)
def add(a, b): return a + b def test_add(): assert add(1, 3) == 1 + 3
This is not a test. No matter what the function name says.
def add(a, b): return a + b @pytest.mark.parametrise('a, b, res', [(1, 2, 1 + 2)]) def test_add(a, b, res): assert add(a, b) == res
Stupid, but not so stupid.
def add(a, b): return a + b @pytest.mark.parametrise('a, b, res', [(1, 2, 3)]) def test_add(a, b, res): assert add(a, b) == res
Also think lookup tables, v. small test data committed along with the code.
This is a vaguely famous mantra.
def test_adding_data(): data = mylibrary.load_data("naturepaper.tsv") data.add(datum) assert data[:-1] == datum # better - decide on the API behavior def test_retrieving_data_added(): """This test is robust if I change how I store `data` under the hood (array or df or whatever)""" data = mylibrary.load_data("naturepaper.tsv") data.add(datum) assert data.get(datum.title) == datum
with pytest.raises(PreciselyTheExceptionYouAreExpecting): do.illegal_thing()
@dataclass class Neuron: activation_threshold: float # mV def test_neuron_object(): n = Neuron(-60) assert n.activation_threshold == pytest.approx(-60.0)
dataclasses.dataclass getters and setters probably don't need testing.
dataclasses.dataclass
git branch
(I'm not really qualified to talk about these too much.)
Simply put, under the test-driven development paradigm, we check โhas the thing been done correctly?โ, whereas under behaviour-driven development we test โhas the correct thing been done?โ.
aesthetic vspace so the title isn't too close to the UCL banner