Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Mihini/Mihini UnitTest

Mihini comes with several unittest facilities.

Lua unittest

we provide unittest.lua file to create new unittest in Lua.


Add a new test

  • Create a new Lua file.
  • require "unittest"
  • create a new testsuite
  • create new test cases
  • special test cases:
    • setup: run before all other test cases of the test suite
    • teardown: run after all other test cases of the test suite


local mymodule = require "mymodule"
 
local u = require "unittest"
--create the test suite
local t = u.newtestsuite("mymodule")
 
-- note that all test must be created like this: testsuitetable:setup, testsuitetable:teardown or testsuitetable:test_TESTNAME,
-- TESTNAME is the only part in test function name that can be changed.
function t:setup();
  u.assert(mymodule.init())
end
 
function t:teardown()
   u.assert(mymodule.stop())
end
 
function t:test_01_feature1()
  u.assert(mymodule.f1())
end

Run a specific test

  • require the Lua file containing the unittest suite
  • do unittest.run()

E.G. using agent telnet:

> telnet localhost 2000
 
> require"mymoduletest"
> unittest.run()


Run all tests

The testsuite to run are kept for each call to newtestsuite function, so just have to require all Lua files containing unittest suites and then do unittest.run().




CMake unittest

The point of this part is to have C unittests, to be integrated easily with CMake (our build system), and run those tests easily: in deed, some test, like C tests can't be easily run in Mihini agent Lua telnet, and can require other/new process, and/or specific tool to be run along side with the test (service providers stubs etc).

CTest is used to do this.

Add a new test

use ADD_UNIT_TEST macro in some CMakeLists.txt file:

ADD_LIBRARY(lib_mylib SHARED mylib.c)
 
ADD_UNIT_TEST(mylibtest mylib_test.c RUNTIME_DEPENDENCIES lib_mylib)

Run all tests

Note: most of existing tests require the agent to be started manually before running them.

This commands runs all tests added with ADD_UNIT_TEST macro : the native tests and some Lua test too.

In build.default folder: (or any build.$target folder), do:

build.default> make test

This commands calls CTest with specific options.

Call CTest manually

build.default> ctest --output-on-failure -j2 --timeout 10 -E foo 
build.default> ctest --output-on-failure -j2 --timeout 10 -R bar
  • "--output-on-failure" -> stop after error, more verbose
  • "-j2" -> runs 2 tests at the same time (related to `make -j` option)
  • "--timeout 10" -> timeout for each individual unittest
  • "-E foo" -> exclude tests matching "foo" pattern
  • "-R bar" -> only run tests matching "bar" pattern

Copyright © Eclipse Foundation, Inc. All Rights Reserved.