Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I come from a Python background, so I'm used to pretty automatic test discovery. For example, the py.test library automatically locates any files which begin with test, finds classes whose names start with Test inside of the those files, and then calls all the functions whose names start with test_ in those classes.

This type of test discovery is enabled by Python's deep support for reflection and dynamism.

These features are often absent in statically typed languages, because they're difficult to make integrate with the type system.

Therefore, I'm interested in hearing about what approaches statically typed languages use to make test discovery simpler?

share|improve this question

closed as too broad by MichaelT, gnat, Dynamic, ChrisF Mar 30 '14 at 18:22

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.If this question can be reworded to fit the rules in the help center, please edit the question.

Not really an answer, but in the D language unit tests are part of the language, with compiler flags and the unittest keyword. – Idan Arye Mar 30 '14 at 15:04

Many statically-typed languages still use reflection for this purpose - for example, JUnit3 calls all methods named testWhatever, and go test looks for methods named TestWhatever() inside any _test.go files.

JUnit4 appears to rely on Java's "annotations" system -- it supports a @Test annotation which is available at runtime and is used for test discovery (

I've personally done some awful, awful things in C using GCC's __attribute__((section)) and the preprocessor to implement a DSL for tests -- see the implementation details. I don't really recommend it.

share|improve this answer

Almost all statically typed language have runtime type and method discovery. The main exception is C/C++.

In the .NET world, reflection and attributes provide a particularly rich environment for test discovery and management, which both Visual Studio and NUnit utilise.

The Java world has something similar, see JUnit.

Those are the "big three". Scala, Haskell and some other related languages are also statically typed, but provide ample support for type discovery.

I guess the static typed world can do just about everything you can do in Python, just a bit differently. C/C++ is a world of its own.

share|improve this answer

Golang handles this by using the parser module to parse the test files, and then generating a new program which runs the test files, compiling that program, and executing it:

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.