Style: include tests in source files?

In a typical Julia package, tests are separated from the source files leading, I’d argue, to the omission of tests and a slower development cycle.

I would rather include function tests in the same file and adjacent to the definitions, for several reasons:

  1. decreased conceptual distance, in that similar things should be near each other, much as single-file components are a useful thing in Vue.

  2. faster development: when tests are in the same module as the definitions, the entire module can be replaced and a running REPL re-used, whereas when tests are in another or global module the source module cannot be replaced and the running REPL must be restarted. (A perhaps needed conversation for another topic is naming the various possible package development workflows and their steps.)

  3. less duplication with Documenter strings: I find that at least some of my function examples are nearly identical to their unit tests. Moreover, unit tests demonstrate the uses and expectations that are at the root of api documentation.

I don’t know if other languages employ approaches like this, Vue’s single-file-components is all that came to mind.

I included “style” in the title because it is certainly possible to define tests in source files and call them right now, but this could be improved with some specific support from or extension to Documenter and Test.

But at the extreme, I might make a doc/function/test/end design, giving first-rate importance to both documentation and unit testing. I realize that this would be a big addition, but as useful as macros are I think documentation and test are more than decorations and that relegating them decreases usage and incorporation in tools. Of course I won’t be surprised if this ship has sailed.

I’d appreciate comments and links to other discussions on this,

You mention Documenter.jl in the third point. Are you aware of doctests?

1 Like

I believe TestItemRunner.jl + TestItems.jl let you put tests in the source code: GitHub - julia-vscode/TestItemRunner.jl: Run Julia test items


Rust does this too: How to Write Tests - The Rust Programming Language

Also C++, e.g. with GitHub - catchorg/Catch2: A modern, C++-native, test framework for unit-tests, TDD and BDD - using C++14, C++17 and later (C++11 support is in v2.x branch, and C++03 on the Catch1.x branch)

Yes, it does. The best intro to the features etc is probably Prerelease of new testing framework and test run UI in VS Code.