test_loader.py 2.4 KB
Newer Older
1 2
import os
import sys
3 4
import tempfile
import shutil
5 6 7 8 9

if sys.version_info[:2] == (2, 6):
    import unittest2 as unittest
else:
    import unittest
10 11

# simple magic for using scripts within a source tree
12
basedir = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', '..')
13 14
basedir = os.path.abspath(basedir)
if os.path.isdir(os.path.join(basedir, 'avocado')):
15
    sys.path.insert(0, basedir)
16 17 18 19 20

from avocado.utils import script
from avocado.utils import process

AVOCADO_TEST_OK = """#!/usr/bin/python
21
from avocado import Test
22
from avocado import main
23

24
class PassTest(Test):
25
    def test(self):
26 27 28
        pass

if __name__ == "__main__":
29
    main()
30 31 32
"""

AVOCADO_TEST_BUGGY = """#!/usr/bin/python
33
from avocado import Test
34
from avocado import main
35 36
import adsh

37
class PassTest(Test):
38
    def test(self):
39 40 41
        pass

if __name__ == "__main__":
42
    main()
43 44 45 46
"""

NOT_A_TEST = """
def hello():
47
    print('Hello World!')
48 49 50 51
"""

PY_SIMPLE_TEST = """#!/usr/bin/python
def hello():
52
    print('Hello World!')
53 54 55 56 57 58 59 60 61 62 63 64

if __name__ == "__main__":
    hello()
"""

SIMPLE_TEST = """#!/bin/sh
true
"""


class LoaderTestFunctional(unittest.TestCase):

65
    def setUp(self):
66
        os.chdir(basedir)
67 68
        self.tmpdir = tempfile.mkdtemp()

69 70 71 72 73 74 75 76 77 78
    def _test(self, name, content, exp_str, mode=0664):
        test_script = script.TemporaryScript(name, content,
                                             'avocado_loader_test',
                                             mode=mode)
        test_script.save()
        cmd_line = ('./scripts/avocado list -V %s' % test_script.path)
        result = process.run(cmd_line)
        self.assertIn('%s: 1' % exp_str, result.stdout)
        test_script.remove()

79
    def test_simple(self):
80
        self._test('simpletest.sh', SIMPLE_TEST, 'SIMPLE', 0775)
81 82

    def test_simple_not_exec(self):
83
        self._test('simpletest.sh', SIMPLE_TEST, 'NOT_A_TEST')
84 85

    def test_pass(self):
86
        self._test('passtest.py', AVOCADO_TEST_OK, 'INSTRUMENTED')
87 88

    def test_buggy_exec(self):
89
        self._test('buggytest.py', AVOCADO_TEST_BUGGY, 'SIMPLE', 0775)
90 91

    def test_buggy_not_exec(self):
92
        self._test('buggytest.py', AVOCADO_TEST_BUGGY, 'BUGGY')
93 94

    def test_load_not_a_test(self):
95
        self._test('notatest.py', NOT_A_TEST, 'SIMPLE', 0775)
96 97

    def test_load_not_a_test_not_exec(self):
98
        self._test('notatest.py', NOT_A_TEST, 'NOT_A_TEST')
99

100 101 102 103
    def tearDown(self):
        shutil.rmtree(self.tmpdir)


104 105
if __name__ == '__main__':
    unittest.main()