I came across a design question when working on elm-test recently:
What if you could write tests in the same file as the code they're testing?
There are a few testing systems that support this:
clojure.spec(to vastly oversimplify) lets you define tests inline in the same file as your business logic.
- Rust allows putting tests in the same file as the code being tested, although the documentation recommends putting them in a separate file (without elaboration as to why).
- Doctests (such as in Python, Elixir, Go, and Haskell) allow writing tests in the same file—theoretically for documentation purposes, although potentially for testing purposes instead.
EUnitin Erlang runs all functions in a given module that end in
_testas tests, including business logic modules.
- Racket supports inline tests.
- Pyret supports "testing blocks" which can live in any source file.
Test::Inlineadds support for this to Perl.
I've never used any of these, but I'm wondering how the experience would be in practice. Is it amazing? Unexciting? An outright bad idea? What are the pros and cons compared to putting tests in a separate file?
If you've tried writing tests like this before, what did you think?