Nox
Download

Tools

Testing

noxc test finds every file named *_test.nox under the current directory, compiles and runs each as its own program, and reports one line per file and a summary. A name argument matches the file name (noxc test smtp runs v18_smtp_test.nox). A directory limits the walk. One .nox path runs that file and returns its exit status. If a file fails to compile, it is reported as failed and the remaining files still run.

Shell
noxc test
noxc test smtp
noxc test tests/http
Text
PASSED: /path/mathutil_test.nox

1 passed, 0 failed (1 total)

(PASSED / FAILED; the exit status is the contract: 0 when every file passes, 1 otherwise.)

Writing tests#

A test file is an ordinary Nox program that fails by raising an exception (or exiting non-zero). Use nox.test:

Nox
import nox.test

def double(n: int) -> int:
    return n * 2

nox.test.assert_eq_int(double(2), 4, "double(2)")
nox.test.assert_true(double(0) == 0, "double(0)")
nox.test.assert_eq_str("a" + "b", "ab", "concat")
print("all good")
Output
all good

For many small checks that should all run even when one fails, use a TestSuite and write a JUnit report for CI:

Nox
suite: TestSuite = TestSuite("mathutil")
suite.check_eq_int("double 2", double(2), 4)
suite.check_eq_int("double 3", double(3), 6)
nox.test.write_junit_xml(suite, "test-results.xml")
if not suite.all_passed():
    nox.os.exit(1)

Importing the code under test#

Tests import your modules like any other code (import mathutil); run noxc test from the project root so the project's nox.json is found.

Testing servers and handlers#

An HTTP handler is just a function: call it with a hand-built HttpRequest and assert on the HttpResponse:

Nox
from nox.http import HttpRequest, HttpResponse
import nox.test

def handle(req: HttpRequest) -> HttpResponse:
    return HttpResponse(200, "hi " + req.target, {})

resp: HttpResponse = handle(HttpRequest("GET", "/x", "", {}, ""))
nox.test.assert_eq_int(resp.status, 200, "status")
nox.test.assert_eq_str(resp.body, "hi /x", "body")
print("ok")
Output
ok

Testing the compiler itself#

The compiler's own suites — golden source→output tests, IR snapshots, differential tests between backends, fuzzing, concurrency torture — are described in Testing internals.