author David Douard <david.douard@logilab.fr>
Wed, 14 May 2014 22:28:57 +0200
changeset 1520 0abe241164e2
parent 1501 a45f5a997ab2
child 1607 51ba8d09c612
permissions -rw-r--r--
[test] fix tests when executed in an apycot environment ie. when tests are executed from sources towards an installed apycot. The fix consist mainly in "moving" the input_path function from testutils.py (which is part of the installed modules) into unittest_checkers.py so we are sure we look for test data files relatively to the test directory (and not the location of the installation).

unit tests for python test checkers

import os, sys
from os.path import join

from logilab.common.testlib import unittest_main, TestCase, mock_object

import cubicweb.devtools
from cubes.apycot.testutils import MockCheckWriter

from checkers.apycot import python

from unittest_checkers import input_path

def _test_cmd(self, cmd, status, success=0, failures=0, errors=0, skipped=0):

    for name, got, expected in (
            ('failures', cmd.parser.failures, failures),
            ('errors', cmd.parser.errors, errors),
            ('skipped', cmd.parser.skipped, skipped),
            ('success', cmd.parser.success, success),
        self.assertEqual(got, expected, '%i %s but %i expected'
                          % (got, name, expected))
    self.assertIs(cmd.status, status)

class PyUnitTestCheckerTC(TestCase):
    input_dir = input_path('testcase_pkg/tests/')

    def setUp(self):
        self.checker = python.PyUnitTestChecker(MockCheckWriter())
        self.checker._path = input_path('')
        self.checker.cwd = self.input_dir

    def input_path(self, path):
        return join(self.input_dir, path)

    def test_run_test_result_empty(self):
        cmd = self.checker.run_test(self.input_path('unittest_empty.py'))
        _test_cmd(self, cmd, NODATA, success=0)

    def test_run_test_result_no_main(self):
        cmd = self.checker.run_test(self.input_path('unittest_no_main.py'))
        _test_cmd(self, cmd, NODATA, success=0)

    def test_run_test_result_success(self):
        cmd = self.checker.run_test(self.input_path('unittest_success.py'))
        _test_cmd(self, cmd, SUCCESS, success=1)

    def test_run_test_result_failure(self):
        cmd = self.checker.run_test(self.input_path('unittest_failure.py'))
        _test_cmd(self, cmd, FAILURE, failures=1)

    def test_run_test_result_error(self):
        cmd = self.checker.run_test(self.input_path('unittest_errors.py'))
        _test_cmd(self, cmd, FAILURE, errors=1)

    def test_run_test_result_skipped(self):
        cmd = self.checker.run_test(self.input_path('unittest_skip.py'))
        _test_cmd(self, cmd, PARTIAL, skipped=1)

    def test_run_test_result_mixed(self):
        cmd = self.checker.run_test(self.input_path('unittest_mixed.py'))
        _test_cmd(self, cmd, FAILURE, 2, 15, 1, 2)

    def test_run_test_result_mixed_std(self):
        cmd = self.checker.run_test(self.input_path('unittest_mixed_std.py'))
        _test_cmd(self, cmd, FAILURE, 2, 15, 1, 0)

class PyTestCheckerTC(TestCase):
    input_dir = input_path('testcase_pkg/tests/')

    def setUp(self):
        self.checker = python.PyTestChecker(MockCheckWriter())
        self.checker.cwd = input_path('testcase_pkg')

    def _test_cmd(self, *args):

        cmd_args = ['-c', 'from logilab.common.pytest '
                  'import run; run()',]
        return self.checker.run_test(cmd_args, sys.executable)

    def test_global(self):
        cmd = self._test_cmd()
        #XXX should be NODATA but not handle at the moment
        _test_cmd(self, cmd, FAILURE, 6, 31, 3, 3)

if __name__ == '__main__':