run 1.7 KB
Newer Older
1 2 3 4 5
#!/usr/bin/env python
# -*- coding: utf-8 -*-

__author__ = 'Lucas Meneghel Rodrigues <lmr@redhat.com>'

6
import os
7
import subprocess
8
import sys
9
import logging
10
import unittest
11

12 13 14
from avocado.core import data_dir


15
logger = logging.getLogger(__name__)
16 17
CHECK_TMP_DIRS = os.path.abspath(os.path.join(os.path.dirname(__file__),
                                              "check_tmp_dirs"))
18 19


20 21 22 23 24 25 26 27 28
def test_suite():
    suite = unittest.TestSuite()
    loader = unittest.TestLoader()
    selftests_dir = os.path.dirname(os.path.abspath(__file__))
    basedir = os.path.dirname(selftests_dir)
    for section in ('unit', 'functional', 'doc'):
        suite.addTests(loader.discover(start_dir=os.path.join(selftests_dir, section),
                                       top_level_dir=basedir))
    return suite
29

L
Lukáš Doktor 已提交
30

31
class MyResult(unittest.TextTestResult):
32 33 34 35
    def stopTest(self, test):
        # stopTest
        ret = super(MyResult, self).stopTest(test)
        # Destroy the data_dir.get_tmpdir ...
36
        data_dir._tmp_tracker.unittest_refresh_dir_tracker()
37 38 39 40 41 42 43
        # ... and check whether some dirs were left behind
        dir_check = subprocess.Popen([CHECK_TMP_DIRS], stdout=subprocess.PIPE,
                                     stderr=subprocess.STDOUT)
        if dir_check.wait():
            raise AssertionError("Test %s left some tmp files behind:\n%s"
                                 % (test, dir_check.stdout.read()))
        return ret
44 45


46
if __name__ == '__main__':
47
    runner = unittest.TextTestRunner(failfast=not os.environ.get("SELF_CHECK_CONTINUOUS"),
48
                                     verbosity=1, resultclass=MyResult)
49 50 51
    result = runner.run(test_suite())
    if result.failures or result.errors:
        sys.exit(1)