TDD-ing a toy sized project
Just recently I was porting a toy sized parser combinator library (a proof of concept) from Haskell to Python. You know, for educational purposes. It turns out I’m not smart enough to keep the complicated types (and explicit laziness) in my head even for such a small project. So my solution was to do TDD. To clarify: I wanted to test happy paths through my functions to make sure at least types fit together.
So tests I will write. But that means I need another file for tests (yes, it
was I single file project, that’s what I mean with “toy-sized”), some actual
structure, a test runner, probably a package description for dependencies…
How about no? I remembered seeing a thing called doctest
once. Turns out it
fit perfectly!.
This is how using doctest
looks in Python:
|
|
And then run with python myfile.py
. It is that simple (and built-int). You
just put examples in the docstring and they will be machine checked.
But I’m lazy and don’t want to run tests every time by hand. I want a
poor-man’s runner with --watch
capability. And I can have it as a bash
one-liner (given that inotify-tools
package is installed on my system).
|
|
This will automatically clear the screen and re-run tests every time I change the file - save it from my editor. Now I can finally develop my toy projects in split screen with terminal and vim using only my editor’s save function to run tests :)
Bonus section
Doctest is ported to Haskell, so I can use it there as well. Just need to
stack install
it globally and it will be available for my one-file projects.
|
|
And run with
|
|
Last modified on 2016-09-21
Previous Repairing a corrupt Git repo using a cloneNext Dockerless Services (with Nix)