test_loader.py 2.3 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 12 13 14
from avocado.utils import script
from avocado.utils import process


15
basedir = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', '..')
16 17 18 19
basedir = os.path.abspath(basedir)


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

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

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

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

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

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

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

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

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

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


class LoaderTestFunctional(unittest.TestCase):

64
    def setUp(self):
65
        os.chdir(basedir)
66
        self.tmpdir = tempfile.mkdtemp(prefix='avocado_' + __name__)
67

68 69 70 71 72 73 74 75 76 77
    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()

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

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

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

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

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

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

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

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


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