Testing

Orange unit testing

This module contains some classes in common use by Orange unit testing framework. In particular its most useful feature is the BaseTestOnData (along with test_on_data function and datasets_driven class decorators) class for automating data driven tests.

Example of use

from Orange.testing import testing
import unittest

data = [("one", 1),
        ("two", 2)]

# Data driven with data_iter argument
# data must be reiterable multiple times if more than one test member defined
@data_driven(data_iter=data)
class TestDemo(unittest.TestCase):
    @test_on_data
    def test_instance_on(self, arg):
        self.assertIsInstance(arg, int)
        
    @test_on_data
    def test_add(self, arg):
        res = arg + arg
        
# data_driven without argument
@data_driven
class TestDemo1(unittest.TestCase):
    @test_on_data(data_iter=data)
    def test_instance_on(self, arg):
        self.assertIsInstance(arg, int)
        
    @test_on_data(data_iter=data)
    def test_add(self, arg):
        res = arg + arg

# data_driven without arg, using a static data_iter method
@data_driven
class TestDemo1(unittest.TestCase):
    @test_on_data
    def test_instance_on(self, arg):
        self.assertIsInstance(arg, int)
        
    @test_on_data
    def test_add(self, arg):
        res = arg + arg
        
    @staticmethod
    def data_iter():
        yield "iris", Orange.data.Table("doc:iris")
    
#@data_driven(data_iter=testing.datasets_iter(testing.CLASSIFICATION_DATASETS | testing.CLASSLES_DATASETS))
@datasets_driven(data_iter=testing.CLASSIFICATION_DATASETS |                     testing.CLASSLESS_DATASETS)
class TestDefaultLearner(unittest.TestCase):
    @test_on_data
    def test_learner_on(self, dataset):
        import Orange
        Orange.classifcation.majority.MajorityLearner(dataset)
        
    # this overloads the class decorator's flags 
    @test_on_datasets(testing.CLASSLES_DATASETS)
    def test_raise_missing_class_on(self, dataset):
        import Orange
        Orange.classifcation.majority.MajorityLearner(dataset)