3

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?

1

3 Answers 3

4

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 (https://github.com/junit-team/junit/wiki/Getting-started).

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.

1

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.

0

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: https://code.google.com/p/go/source/browse/src/cmd/go/test.go#1022

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