Test unit generation and execution for Nit.

In Nit, unit testing can be achieved in two ways:

  • using DocUnits in code comments
  • using TestSuites with test unit files

DocUnits are executable pieces of code found in the documentation of modules, classes and properties. They are used for documentation purpose, they should be kept simple and illustrative. More advanced unit testing can be done using TestSuites.

TestSuites are test files coupled to a tested module. They contain a list of test methods called TestCase.

Working with DocUnits

With DocUnits, executable code can be placed in comments of modules, classes and properties. The execution can be verified using assert

Example with a class:

module foo

#    var foo = new Foo
#    assert foo.bar == 10
class Foo
    var bar = 10
end

Everything used in the test must be declared. To test a method you have to instanciate its class:

module foo

#    var foo = new Foo
#    assert foo.bar == 10
class Foo
    #    var foo = new Foo
    #    assert foo.baz(1, 2) == 3
    fun baz(a, b: Int): Int do return a + b
end

nitunit is used to test Nit files:

    $ nitunit foo.nit

Working with TestSuites

TestSuites are Nit files that define a set of TestCase for a particular module.

The test suite module must be declared using the test annotation. The structure of a test suite is the following:

# test suite for module `foo`
module test_foo is test

import foo # can be intrude to test private things

class TestFoo
    test

    # test case for `foo::Foo::baz`
    fun baz is test do
        var subject = new Foo
        assert subject.baz(1, 2) == 3
    end
end

Test suite can be executed using the same nitunit command:

    $ nitunit foo.nit

To be started automatically with nitunit, the module must be called test_ followed by the name of the module to test. So for the module foo.nit the test suite will be called test_foo.nit. Otherwise, you can use the -t option to specify the test suite module name:

    $ nitunit foo.nit -t my_test_suite.nit

nitunit will execute a test for each method annotated with test in a class also annotated with test so multiple tests can be executed for a single method:

class TestFoo
    test

    fun baz_1 is test do
        var subject = new Foo
        assert subject.baz(1, 2) == 3
    end

    fun baz_2 is test do
        var subject = new Foo
        assert subject.baz(1, -2) == -1
    end
end

TestSuites also provide methods to configure the test run:

before and after annotations can be added to methods that must be called before/after each test case. They can be used to factorize repetitive tasks:

class TestFoo
    test

    var subject: Foo is noinit

    # Method executed before each test
    fun set_up is before do
        subject = new Foo
    end

    fun baz_1 is test do
        assert subject.baz(1, 2) == 3
    end

    fun baz_2 is test do
        assert subject.baz(1, -2) == -1
    end
end

When using custom test attributes, a empty init must be declared to allow automatic test running.

At class level, before_all and after_all annotations can be set on methods that must be called before/after all the test cases in the class:

class TestFoo
    test

    var subject: Foo is noinit

    # Method executed before all tests in the class
    fun set_up is before_all do
        subject = new Foo
    end

    fun baz_1 is test do
        assert subject.baz(1, 2) == 3
    end

    fun baz_2 is test do
        assert subject.baz(1, -2) == -1
    end
end

before_all and after_all annotations can also be set on methods that must be called before/after each test suite when declared at top level:

module test_bdd_connector

import bdd_connector

# Testing the bdd_connector
class TestConnector
    test
    # test cases using a server
end

# Method executed before testing the module
fun setup_db is before_all do
    # start server before all test cases
end

# Method executed after testing the module
fun teardown_db is after_all do
    # stop server after all test cases
end

When dealing with multiple test suites, niunit allows you to import other test suites to factorize your tests:

module test_bdd_users

import test_bdd_connector

# Testing the user table
class TestUsersTable
    test
    # test cases using the db server from `test_bdd_connector`
end

fun setup_table is before_all do
    # create user table
end

fun teardown_table is after_all do
    # drop user table
end

Methods with before* and after* annotations are linearized and called in different ways.

  • before* methods are called from the least specific to the most specific
  • after* methods are called from the most specific to the least specific

In the previous example, the execution order would be:

  1. test_bdd_connector::setup_db
  2. test_bdd_users::setup_table
  3. all test cases from test_bdd_users
  4. test_bdd_users::teardown_table
  5. test_bdd_connector::teardown_db

Generating test suites

Write test suites for big modules can be a pepetitive and boring task... To make it easier, nitunit can generate test skeletons for Nit modules:

    $ nitunit --gen-suite foo.nit

This will generate the test suite test_foo containing test case stubs for all public methods found in foo.nit.

Useful options with --gen-suite:

  • --private: also generate tests for protected and private methods
  • --force: force generation of the skeleton (existing test suite will be overwritten)

All subgroups and modules

module testing

nitc :: testing

Test unit generation and execution for Nit.
module testing_base

nitc :: testing_base

Base options for testing tools.
module testing_doc

nitc :: testing_doc

Testing from code comments.
module testing_gen

nitc :: testing_gen

Test Suites generation.
module testing_suite

nitc :: testing_suite

Testing from external files.
package_diagram nitc\>testing\> testing nitc\>modelize\> modelize nitc\>testing\>->nitc\>modelize\> nitc nitc nitc\>testing\>->nitc html html nitc\>testing\>->html markdown2 markdown2 nitc\>testing\>->markdown2 realtime realtime nitc\>testing\>->realtime nitc\>frontend\> frontend nitc\>testing\>->nitc\>frontend\> nitc\>modelize\>->nitc template template html->template core core html->core markdown2->template markdown2->core config config markdown2->config json json markdown2->json realtime->core nitc\>frontend\>->nitc\>modelize\> nitc\>frontend\>->nitc gen_nit gen_nit nitc\>frontend\>->gen_nit nitc\>semantize\> semantize nitc\>frontend\>->nitc\>semantize\> nitc\>parser\> parser nitc\>frontend\>->nitc\>parser\> ...nitc ... ...nitc->nitc ...template ... ...template->template ...core ... ...core->core ...config ... ...config->config ...json ... ...json->json ...gen_nit ... ...gen_nit->gen_nit ...nitc\>semantize\> ... ...nitc\>semantize\>->nitc\>semantize\> ...nitc\>parser\> ... ...nitc\>parser\>->nitc\>parser\>

Ancestors

group api

nitc > doc > api

Components required to build a web server about the nit model.
group base64

base64

Offers the base 64 encoding and decoding algorithms
group c

c

Structures and services for compatibility with the C language
group catalog

nitc > catalog

Basic catalog generator for Nit packages
group codecs

core > codecs

Group module for all codec-related manipulations
group collection

core > collection

This module define several collection classes.
group commands

nitc > doc > commands

group compiler

nitc > compiler

Compilation to C
group compiler_ffi

nitc > compiler > compiler_ffi

Full FFI support for the compiler
group config

config

Configuration options for nit tools and apps
group console

console

Defines some ANSI Terminal Control Escape Sequences.
group core

core

Nit common library of core classes and methods
group counter

counter

Simple numerical statistical analysis and presentation
group csv

csv

CSV document handling.
group curl

curl

Data transfer powered by the native curl library
group doc

nitc > doc

group dot

dot

Dot rendering library
group dynamic_loading_ffi

nitc > interpreter > dynamic_loading_ffi

This group implement a partial support for the Nit FFI in the interpreter.
group ffi

nitc > ffi

Full FFI support, independent of the compiler
group gen_nit

gen_nit

Support to generate and otherwise manipulate Nit code
group github

github

Nit wrapper for Github API
group graph

graph

group ini

ini

ini - Read and write INI configuration files
group interpreter

nitc > interpreter

Interpretation of Nit programs
group json

json

read and write JSON formatted text
group libevent

libevent

Low-level wrapper around the libevent library to manage events on file descriptors
group logger

logger

A simple logger for Nit
group markdown

markdown

A markdown parser for Nit.
group md5

md5

Native MD5 digest implementation as Text::md5
group meta

meta

Simple user-defined meta-level to manipulate types of instances as object.
group metrics

nitc > metrics

Various statistics about Nit models and programs
group model

nitc > model

The meta model of Nit programs
group mongodb

mongodb

MongoDB Nit Driver.
group more_collections

more_collections

Highly specific, but useful, collections-related classes.
group neo4j

neo4j

Neo4j connector through its JSON REST API using curl.
group nitcorn

nitcorn

Lightweight framework for Web applications development
group nitni

nitc > nitni

Native interface related services (used underneath the FFI)
group opts

opts

Management of options on the command line
group ordered_tree

ordered_tree

Manipulation and presentation of ordered trees.
group parser

nitc > parser

Parser and AST for the Nit language
group parser_base

parser_base

Simple base for hand-made parsers of all kinds
group perfect_hashing

perfect_hashing

Perfect hashing and perfect numbering
group performance_analysis

performance_analysis

Services to gather information on the performance of events by categories
group pipeline

pipeline

Pipelined filters and operations on iterators.
group platform

nitc > platform

Platform system, used to customize the behavior of the compiler.
group popcorn

popcorn

Popcorn
group poset

poset

Pre order sets and partial order set (ie hierarchies)
group prompt

prompt

Basic services to display a prompt
group pthreads

pthreads

POSIX Threads support
group saf

nitc > saf

Nit Static Analysis Framework.
group semantize

nitc > semantize

Process bodies of methods in regard with the model.
group serialization

serialization

Abstract serialization services
group static

nitc > doc > static

Nitdoc generation framework
group template

template

Basic template system
group term

nitc > doc > term

group text

core > text

All the classes and methods related to the manipulation of text entities
group trees

trees

General module for tree data structures
group uml

nitc > uml

Group head module for UML generation services
group vm

nitc > vm

Entry point of all vm components

Parents

group frontend

nitc > frontend

Collect and orchestration of main frontend phases
group html

html

HTML output facilities
group modelize

nitc > modelize

Create a model from nit source files
group nitc

nitc

Nit compiler and tools
group realtime

realtime

Services to keep time of the wall clock time

Children

group nitc

nitc

Nit compiler and tools

Descendants

group api

nitc > doc > api

Components required to build a web server about the nit model.
group catalog

nitc > catalog

Basic catalog generator for Nit packages
group commands

nitc > doc > commands

group compiler

nitc > compiler

Compilation to C
group compiler_ffi

nitc > compiler > compiler_ffi

Full FFI support for the compiler
group doc

nitc > doc

group dynamic_loading_ffi

nitc > interpreter > dynamic_loading_ffi

This group implement a partial support for the Nit FFI in the interpreter.
group ffi

nitc > ffi

Full FFI support, independent of the compiler
group frontend

nitc > frontend

Collect and orchestration of main frontend phases
group interpreter

nitc > interpreter

Interpretation of Nit programs
group metrics

nitc > metrics

Various statistics about Nit models and programs
group model

nitc > model

The meta model of Nit programs
group modelize

nitc > modelize

Create a model from nit source files
group nitni

nitc > nitni

Native interface related services (used underneath the FFI)
group parser

nitc > parser

Parser and AST for the Nit language
group platform

nitc > platform

Platform system, used to customize the behavior of the compiler.
group saf

nitc > saf

Nit Static Analysis Framework.
group semantize

nitc > semantize

Process bodies of methods in regard with the model.
group static

nitc > doc > static

Nitdoc generation framework
group term

nitc > doc > term

group uml

nitc > uml

Group head module for UML generation services
group vm

nitc > vm

Entry point of all vm components