test_loader.py 4.1 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
basedir = os.path.abspath(basedir)


20
AVOCADO_TEST_OK = """#!/usr/bin/env 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
AVOCADO_TEST_SLEEP_ELEVEN = """#!/usr/bin/env python
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
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
AVOCADO_TEST_MULTIPLE_CLASSES = """#!/usr/bin/env python
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
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
PY_SIMPLE_TEST = """#!/usr/bin/env python
77
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
    @unittest.skip("Temporary plugin infrastructure removal")
106
    def test_simple(self):
107
        self._test('simpletest.sh', SIMPLE_TEST, 'SIMPLE', 0775)
108

109
    @unittest.skip("Temporary plugin infrastructure removal")
110
    def test_simple_not_exec(self):
111
        self._test('simpletest.sh', SIMPLE_TEST, 'NOT_A_TEST')
112

113
    @unittest.skip("Temporary plugin infrastructure removal")
114
    def test_pass(self):
115
        self._test('passtest.py', AVOCADO_TEST_OK, 'INSTRUMENTED')
116

117
    @unittest.skip("Temporary plugin infrastructure removal")
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
    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)

139
    @unittest.skip("Temporary plugin infrastructure removal")
140 141 142 143
    def test_multiple_class(self):
        self._test('multipleclasses.py', AVOCADO_TEST_MULTIPLE_CLASSES,
                   'INSTRUMENTED', 0664, 2)

144
    @unittest.skip("Temporary plugin infrastructure removal")
145
    def test_load_not_a_test(self):
146
        self._test('notatest.py', NOT_A_TEST, 'SIMPLE', 0775)
147

148
    @unittest.skip("Temporary plugin infrastructure removal")
149
    def test_load_not_a_test_not_exec(self):
150
        self._test('notatest.py', NOT_A_TEST, 'NOT_A_TEST')
151

152 153 154 155
    def tearDown(self):
        shutil.rmtree(self.tmpdir)


156 157
if __name__ == '__main__':
    unittest.main()