summaryrefslogtreecommitdiff
path: root/buildscripts/tests/resmokelib/testing/testcases/test_pytest.py
blob: 6d349564abbf0f4976a8375a46ee77c32ba782f2 (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
"""Unit tests for the buildscripts.resmokelib.testing.testcases.pytest module."""
import logging
import sys
import unittest

from buildscripts.resmokelib.testing.testcases import pytest

_IS_WINDOWS = sys.platform == "win32"

# pylint: disable=missing-docstring,protected-access


def get_filename(filename):
    if _IS_WINDOWS:
        return filename.replace("/", "\\")
    return filename


class TestPyTestCase(unittest.TestCase):
    def test__make_process(self):
        logger = logging.getLogger("pytest")
        filename = "myfile.py"
        pytest_case = pytest.PyTestCase(logger, filename)
        self.assertEqual(pytest_case.test_name, filename)
        self.assertEqual(pytest_case.logger, logger)
        proc = pytest_case._make_process()
        self.assertIn(sys.executable + " -m unittest", proc.as_command())
        self.assertIn(pytest_case.test_module_name, proc.as_command())
        self.assertEqual(proc.logger, logger)

    def test__make_process_dir(self):
        logger = logging.getLogger("pytest")
        filename = "dir1/dir2/myfile.py"
        pytest_case = pytest.PyTestCase(logger, filename)
        self.assertEqual(pytest_case.test_name, filename)
        self.assertEqual(pytest_case.logger, logger)
        proc = pytest_case._make_process()
        self.assertIn(sys.executable + " -m unittest", proc.as_command())
        self.assertIn(pytest_case.test_module_name, proc.as_command())
        self.assertEqual(proc.logger, logger)

    def test__make_process_windows_file(self):
        logger = logging.getLogger("pytest")
        filename = "dir1\\dir2\\myfile.py"
        pytest_case = pytest.PyTestCase(logger, filename)
        self.assertEqual(pytest_case.test_name, filename)
        self.assertEqual(pytest_case.logger, logger)
        proc = pytest_case._make_process()
        self.assertIn(sys.executable + " -m unittest", proc.as_command())
        self.assertIn(pytest_case.test_module_name, proc.as_command())
        self.assertEqual(proc.logger, logger)

    def test_test_module_name(self):
        logger = logging.getLogger("pytest")
        filename = get_filename("dir1/dir2/dir3/myfile.py")
        pytest_case = pytest.PyTestCase(logger, filename)
        self.assertEqual(pytest_case.test_module_name, "dir1.dir2.dir3.myfile")

    def test_test_module_name_absolute_dir(self):
        logger = logging.getLogger("pytest")
        filename = get_filename("/dir1/dir2/dir3/myfile.py")
        pytest_case = pytest.PyTestCase(logger, filename)
        self.assertEqual(pytest_case.test_module_name, ".dir1.dir2.dir3.myfile")

    def test_test_module_name_no_dir(self):
        logger = logging.getLogger("pytest")
        filename = "myfile.py"
        pytest_case = pytest.PyTestCase(logger, filename)
        self.assertEqual(pytest_case.test_module_name, "myfile")