How to create unitary tests

From KratosWiki
Revision as of 11:49, 26 February 2016 by Pooyan (Talk | contribs)
Jump to: navigation, search

Under construction

Kratos Multiphysics has a mechanism to automatically test your code called "KratosUnittest". If you are familiar with python, this module is basically an extension of Unittest and you can expect to find every functionality that exists on in as well in KratosUnittest.

In order for your application to be robust, is recommemded that you add unittests to it. Here we present some guidelines on how to do it.

How to run tests

Kratos unittest are executed using the "run_tests.py" script located in the "kratos/kratos/python_scripts" folder.

Usage is the following:

python run_tests.py

you can specify the following options:

-l,--level:        Select the suit. Values: "All", "Nighlty", "Small"(default)
-v,--vervosity:    Select the vervosity level of the output. Values: 0, 1 (default) , 2
-a,--applications: List of applications to run separated by ":". For example "-a KratosKore:IncompresibleFluidApplication"
                   All applications compiled are run by default.

Basic structure

Tests are defined in a python script. To keep tests organized we recommend you to create the tests in your "application/tests/" directory.

Tests are organized in suites. Suites are collection of tests that will be run together as a package. In Kratos, we define three basic suites:

  • All: which should contain all the tests
  • Nighlty: which should contain a set of tests that could be executed in less than 10 min
  • Small: which should contain a set of tests that could be executed in less than 1 min


All applications should implement this packages as they can be automatically run, for example in the "nighlty" runs. In order to add tests you should create at least a couple of files, one to define the tests

and one to define the suits:

"application/tests/test_NAME_OF_OUR_APPLICATION.py"


for example:

kratos/applications/example_application/tests/test_example_application.py


This file will define the suites to run the tests. We define three different levels of suites in kratos:

  • All: which should contain all the tests
  • Nighlty: which should contain a set of tests that could be executed in less than 10 min
  • Small: which should contain a set of tests that could be executed in less than 1 min


In order to add test to some of these suits, one can do it as shown in this example:

from __future__ import print_function, absolute_import, division

# import Kratos
from KratosMultiphysics import *

# Import Kratos "wrapper" for unittests
import KratosMultiphysics.KratosUnittest as KratosUnittest

# Import the tests o test_classes to create the suites. For example
from test_my_app_example_tests_1 import TestCase1 as TestCase1
from test_my_app_example_tests_2 import TestCase2 as TestCase2

def AssambleTestSuites():
     Populates the test suites to run.
    
    Populates the test suites to run. At least, it should pupulate the suites:
    "small", "nighlty" and "all"
    
    Return
    ------
    
    suites: A dictionary of suites
        The set of suites with its test_cases added.
    
    
    # Get the already defined suits
    suites = KratosUnittest.KratosSuites
    
    # Get the small suit and populate it with some tests from TestCase1 and TestCase2
    smallSuite = suites['small']
    smallSuite.addTest(TestCase1('test_example_small_boo_1'))
    smallSuite.addTest(TestCase2('test_example_small_foo_1'))
    
    # Get the small suit and populate it with some tests from TestCase1 and TestCase2
    nightSuite = suites['nightly']
    smallSuite.addTest(TestCase1('test_example_nightly_boo_1'))
    smallSuite.addTest(TestCase1('test_example_nightly_boo_2'))
    smallSuite.addTest(TestCase2('test_example_nightly_foo_1'))
    
    # Get the small suit and populate it with all tests from TestCase1 and TestCase2
    allSuite = suites['all']
    allSuite.addTests(
        KratosUnittest.TestLoader().loadTestsFromTestCases([
            TestCase1,
            TestCase2
        ])
    )
    
    # Return the suites
    return suites
    
# The main function executes the tests
if __name__ == '__main__':
    KratosUnittest.runTests(AssambleTestSuites())
Personal tools
Categories