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

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

12 13 14 15
from avocado.utils import script
from avocado.utils import process


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


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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

AVOCADO_TEST_SLEEP_ELEVEN = """#!/usr/bin/python
import time

from avocado import Test
from avocado import main

class SleepEleven(Test):
    def test(self):
        time.sleep(10)
    def test_2(self):
        time.sleep(1)

time.sleep(11)

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


52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
AVOCADO_TEST_MULTIPLE_CLASSES = """#!/usr/bin/python
import time

from avocado import Test
from avocado import main

class First(Test):
    def test(self):
        pass

class Second(Test):
    def test(self):
        pass

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


71 72
NOT_A_TEST = """
def hello():
73
    print('Hello World!')
74 75 76 77
"""

PY_SIMPLE_TEST = """#!/usr/bin/python
def hello():
78
    print('Hello World!')
79 80 81 82 83 84 85 86 87 88 89 90

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

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


class LoaderTestFunctional(unittest.TestCase):

91
    def setUp(self):
92
        os.chdir(basedir)
93
        self.tmpdir = tempfile.mkdtemp(prefix='avocado_' + __name__)
94

95
    def _test(self, name, content, exp_str, mode=0664, count=1):
96 97 98 99 100 101
        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)
102
        self.assertIn('%s: %s' % (exp_str, count), result.stdout)
103 104
        test_script.remove()

105
    def test_simple(self):
106
        self._test('simpletest.sh', SIMPLE_TEST, 'SIMPLE', 0775)
107 108

    def test_simple_not_exec(self):
109
        self._test('simpletest.sh', SIMPLE_TEST, 'NOT_A_TEST')
110 111

    def test_pass(self):
112
        self._test('passtest.py', AVOCADO_TEST_OK, 'INSTRUMENTED')
113

114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
    def test_sleep_a_lot(self):
        """
        Verifies that the test loader, at list time, does not load the Python
        module and thus executes its contents.
        """
        test_script = script.TemporaryScript('sleepeleven.py',
                                             AVOCADO_TEST_SLEEP_ELEVEN,
                                             'avocado_loader_test',
                                             mode=0664)
        test_script.save()
        cmd_line = ('./scripts/avocado list -V %s' % test_script.path)
        initial_time = time.time()
        result = process.run(cmd_line, ignore_status=True)
        test_script.remove()
        actual_time = time.time() - initial_time
        self.assertLess(actual_time, 3.0,
                        ("Took more than 3 seconds to list tests. Loader "
                         "probably loaded/executed Python code and slept for "
                         "eleven seconds."))
        self.assertIn('INSTRUMENTED: 2', result.stdout)

135 136 137 138
    def test_multiple_class(self):
        self._test('multipleclasses.py', AVOCADO_TEST_MULTIPLE_CLASSES,
                   'INSTRUMENTED', 0664, 2)

139
    def test_load_not_a_test(self):
140
        self._test('notatest.py', NOT_A_TEST, 'SIMPLE', 0775)
141 142

    def test_load_not_a_test_not_exec(self):
143
        self._test('notatest.py', NOT_A_TEST, 'NOT_A_TEST')
144

145 146 147 148
    def tearDown(self):
        shutil.rmtree(self.tmpdir)


149 150
if __name__ == '__main__':
    unittest.main()