summaryrefslogtreecommitdiff
path: root/test/test_pytest.py
blob: fb95c2325110a0c1039f23fdeba25c6f0f3c680c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
#
# This file is part of logilab-common.
#
# logilab-common is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 2.1 of the License, or (at your option) any
# later version.
#
# logilab-common is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License along
# with logilab-common.  If not, see <http://www.gnu.org/licenses/>.
import sys

from os.path import join
from logilab.common.testlib import TestCase, unittest_main
from logilab.common.pytest import (
    this_is_a_testdir,
    this_is_a_testfile,
    replace_trace,
    pause_trace,
    nocoverage,
)


class ModuleFunctionTC(TestCase):
    def test_this_is_testdir(self):
        self.assertTrue(this_is_a_testdir("test"))
        self.assertTrue(this_is_a_testdir("tests"))
        self.assertTrue(this_is_a_testdir("unittests"))
        self.assertTrue(this_is_a_testdir("unittest"))
        self.assertFalse(this_is_a_testdir("unit"))
        self.assertFalse(this_is_a_testdir("units"))
        self.assertFalse(this_is_a_testdir("undksjhqfl"))
        self.assertFalse(this_is_a_testdir("this_is_not_a_dir_test"))
        self.assertFalse(this_is_a_testdir("this_is_not_a_testdir"))
        self.assertFalse(this_is_a_testdir("unittestsarenothere"))
        self.assertTrue(this_is_a_testdir(join("coincoin", "unittests")))
        self.assertFalse(this_is_a_testdir(join("unittests", "spongebob")))

    def test_this_is_testfile(self):
        self.assertTrue(this_is_a_testfile("test.py"))
        self.assertTrue(this_is_a_testfile("testbabar.py"))
        self.assertTrue(this_is_a_testfile("unittest_celestine.py"))
        self.assertTrue(this_is_a_testfile("smoketest.py"))
        self.assertFalse(this_is_a_testfile("test.pyc"))
        self.assertFalse(this_is_a_testfile("zephir_test.py"))
        self.assertFalse(this_is_a_testfile("smoketest.pl"))
        self.assertFalse(this_is_a_testfile("unittest"))
        self.assertTrue(this_is_a_testfile(join("coincoin", "unittest_bibi.py")))
        self.assertFalse(this_is_a_testfile(join("unittest", "spongebob.py")))

    def test_replace_trace(self):
        def tracefn(frame, event, arg):
            pass

        oldtrace = sys.gettrace()
        with replace_trace(tracefn):
            self.assertIs(sys.gettrace(), tracefn)

        self.assertIs(sys.gettrace(), oldtrace)

    def test_pause_trace(self):
        def tracefn(frame, event, arg):
            pass

        oldtrace = sys.gettrace()
        sys.settrace(tracefn)
        try:
            self.assertIs(sys.gettrace(), tracefn)
            with pause_trace():
                self.assertIs(sys.gettrace(), None)
            self.assertIs(sys.gettrace(), tracefn)
        finally:
            sys.settrace(oldtrace)

    def test_nocoverage(self):
        def tracefn(frame, event, arg):
            pass

        @nocoverage
        def myfn():
            self.assertIs(sys.gettrace(), None)

        with replace_trace(tracefn):
            myfn()


if __name__ == "__main__":
    unittest_main()