Пример #1
0
    def test_nested_suite(self):
        stub = Stub()
        discovered = StubDiscoveredTests(stub)
        session = StubPytestSession(stub)
        testroot = fix_path('/a/b/c')
        relfile = fix_path('x/y/z/test_eggs.py')
        session.items = [
            StubFunctionItem(
                stub,
                nodeid=relfile + '::SpamTests::Ham::Eggs::test_spam',
                name='test_spam',
                location=(relfile, 12, 'SpamTests.Ham.Eggs.test_spam'),
                fspath=PATH_JOIN(testroot, relfile),
                function=FakeFunc('test_spam'),
                ),
            ]
        collector = TestCollector(tests=discovered)

        collector.pytest_collection_finish(session)

        self.maxDiff = None
        self.assertEqual(stub.calls, [
            ('discovered.reset', None, None),
            ('discovered.add_test', None, dict(
                parents=[
                    ('./x/y/z/test_eggs.py::SpamTests::Ham::Eggs', 'Eggs', 'suite'),
                    ('./x/y/z/test_eggs.py::SpamTests::Ham', 'Ham', 'suite'),
                    ('./x/y/z/test_eggs.py::SpamTests', 'SpamTests', 'suite'),
                    ('./x/y/z/test_eggs.py', 'test_eggs.py', 'file'),
                    ('./x/y/z', 'z', 'folder'),
                    ('./x/y', 'y', 'folder'),
                    ('./x', 'x', 'folder'),
                    ('.', testroot, 'folder'),
                    ],
                test=TestInfo(
                    id='./x/y/z/test_eggs.py::SpamTests::Ham::Eggs::test_spam',
                    name='test_spam',
                    path=TestPath(
                        root=testroot,
                        relfile=fix_relpath(relfile),
                        func='SpamTests.Ham.Eggs.test_spam',
                        sub=None,
                        ),
                    source='{}:{}'.format(fix_relpath(relfile), 13),
                    markers=None,
                    parentid='./x/y/z/test_eggs.py::SpamTests::Ham::Eggs',
                    ),
                )),
            ])
Пример #2
0
 def test_fix_relpath(self):
     tests = [
         ("spam.py", posixpath, "./spam.py"),
         ("eggs/spam.py", posixpath, "./eggs/spam.py"),
         ("eggs/spam/", posixpath, "./eggs/spam/"),
         (r"\spam.py", posixpath, r"./\spam.py"),
         ("spam.py", ntpath, r".\spam.py"),
         (r"eggs\spam.py", ntpath, ".\eggs\spam.py"),
         ("eggs\\spam\\", ntpath, ".\\eggs\\spam\\"),
         ("/spam.py", ntpath, r"\spam.py"),  # Note the fixed "/".
         # absolute
         ("/", posixpath, "/"),
         ("/spam.py", posixpath, "/spam.py"),
         ("\\", ntpath, "\\"),
         (r"\spam.py", ntpath, r"\spam.py"),
         (r"C:\spam.py", ntpath, r"C:\spam.py"),
         # no-op
         ("./spam.py", posixpath, "./spam.py"),
         (r".\spam.py", ntpath, r".\spam.py"),
     ]
     # no-op
     for path in [".", ".."]:
         tests.extend(
             [(path, posixpath, path), (path, ntpath, path),]
         )
     for path, _os_path, expected in tests:
         with self.subTest((path, _os_path.sep)):
             fixed = fix_relpath(
                 path,
                 _fix_path=(lambda p: fix_path(p, _pathsep=_os_path.sep)),
                 _path_isabs=_os_path.isabs,
                 _pathsep=_os_path.sep,
             )
             self.assertEqual(fixed, expected)
Пример #3
0
 def test_fix_relpath(self):
     tests = [
         ('spam.py', posixpath, './spam.py'),
         ('eggs/spam.py', posixpath, './eggs/spam.py'),
         ('eggs/spam/', posixpath, './eggs/spam/'),
         (r'\spam.py', posixpath, r'./\spam.py'),
         ('spam.py', ntpath, r'.\spam.py'),
         (r'eggs\spam.py', ntpath, '.\eggs\spam.py'),
         ('eggs\\spam\\', ntpath, '.\\eggs\\spam\\'),
         ('/spam.py', ntpath, r'\spam.py'),  # Note the fixed "/".
         # absolute
         ('/', posixpath, '/'),
         ('/spam.py', posixpath, '/spam.py'),
         ('\\', ntpath, '\\'),
         (r'\spam.py', ntpath, r'\spam.py'),
         (r'C:\spam.py', ntpath, r'C:\spam.py'),
         # no-op
         ('./spam.py', posixpath, './spam.py'),
         (r'.\spam.py', ntpath, r'.\spam.py'),
     ]
     # no-op
     for path in ['.', '..']:
         tests.extend([
             (path, posixpath, path),
             (path, ntpath, path),
         ])
     for path, _os_path, expected in tests:
         with self.subTest((path, _os_path.sep)):
             fixed = fix_relpath(
                 path,
                 _fix_path=(lambda p: fix_path(p, _pathsep=_os_path.sep)),
                 _path_isabs=_os_path.isabs,
                 _pathsep=_os_path.sep,
             )
             self.assertEqual(fixed, expected)
Пример #4
0
    def test_nested_suite_simple(self):
        testroot = fix_path('/a/b/c')
        relfile = fix_path('./test_eggs.py')
        alltests = [
            TestInfo(
                id=relfile + '::TestOuter::TestInner::test_spam',
                name='test_spam',
                path=TestPath(
                    root=testroot,
                    relfile=relfile,
                    func='TestOuter.TestInner.test_spam',
                ),
                source='{}:{}'.format(relfile, 10),
                markers=None,
                parentid=relfile + '::TestOuter::TestInner',
            ),
            TestInfo(
                id=relfile + '::TestOuter::TestInner::test_eggs',
                name='test_eggs',
                path=TestPath(
                    root=testroot,
                    relfile=relfile,
                    func='TestOuter.TestInner.test_eggs',
                ),
                source='{}:{}'.format(relfile, 21),
                markers=None,
                parentid=relfile + '::TestOuter::TestInner',
            ),
        ]
        allparents = [
            [
                (relfile + '::TestOuter::TestInner', 'TestInner', 'suite'),
                (relfile + '::TestOuter', 'TestOuter', 'suite'),
                (relfile, 'test_eggs.py', 'file'),
                ('.', testroot, 'folder'),
            ],
            [
                (relfile + '::TestOuter::TestInner', 'TestInner', 'suite'),
                (relfile + '::TestOuter', 'TestOuter', 'suite'),
                (relfile, 'test_eggs.py', 'file'),
                ('.', testroot, 'folder'),
            ],
        ]
        expected = [
            test._replace(id=_fix_nodeid(test.id),
                          parentid=_fix_nodeid(test.parentid))
            for test in alltests
        ]

        discovered = DiscoveredTests()
        for test, parents in zip(alltests, allparents):
            discovered.add_test(test, parents)
        tests = list(discovered)
        parents = discovered.parents

        self.maxDiff = None
        self.assertEqual(tests, expected)
        self.assertEqual(parents, [
            ParentInfo(
                id='.',
                kind='folder',
                name=testroot,
            ),
            ParentInfo(id='./test_eggs.py',
                       kind='file',
                       name='test_eggs.py',
                       root=testroot,
                       relpath=fix_relpath(relfile),
                       parentid='.'),
            ParentInfo(
                id='./test_eggs.py::TestOuter',
                kind='suite',
                name='TestOuter',
                root=testroot,
                parentid='./test_eggs.py',
            ),
            ParentInfo(
                id='./test_eggs.py::TestOuter::TestInner',
                kind='suite',
                name='TestInner',
                root=testroot,
                parentid='./test_eggs.py::TestOuter',
            ),
        ])
Пример #5
0
    def test_doctest(self):
        testroot = fix_path('/a/b/c')
        doctestfile = fix_path('./x/test_doctest.txt')
        relfile = fix_path('./x/y/z/test_eggs.py')
        alltests = [
            TestInfo(
                id=doctestfile + '::test_doctest.txt',
                name='test_doctest.txt',
                path=TestPath(
                    root=testroot,
                    relfile=doctestfile,
                    func=None,
                ),
                source='{}:{}'.format(doctestfile, 0),
                markers=[],
                parentid=doctestfile,
            ),
            # With --doctest-modules
            TestInfo(
                id=relfile + '::test_eggs',
                name='test_eggs',
                path=TestPath(
                    root=testroot,
                    relfile=relfile,
                    func=None,
                ),
                source='{}:{}'.format(relfile, 0),
                markers=[],
                parentid=relfile,
            ),
            TestInfo(
                id=relfile + '::test_eggs.TestSpam',
                name='test_eggs.TestSpam',
                path=TestPath(
                    root=testroot,
                    relfile=relfile,
                    func=None,
                ),
                source='{}:{}'.format(relfile, 12),
                markers=[],
                parentid=relfile,
            ),
            TestInfo(
                id=relfile + '::test_eggs.TestSpam.TestEggs',
                name='test_eggs.TestSpam.TestEggs',
                path=TestPath(
                    root=testroot,
                    relfile=relfile,
                    func=None,
                ),
                source='{}:{}'.format(relfile, 27),
                markers=[],
                parentid=relfile,
            ),
        ]
        allparents = [
            [
                (doctestfile, 'test_doctest.txt', 'file'),
                (fix_path('./x'), 'x', 'folder'),
                ('.', testroot, 'folder'),
            ],
            [
                (relfile, 'test_eggs.py', 'file'),
                (fix_path('./x/y/z'), 'z', 'folder'),
                (fix_path('./x/y'), 'y', 'folder'),
                (fix_path('./x'), 'x', 'folder'),
                ('.', testroot, 'folder'),
            ],
            [
                (relfile, 'test_eggs.py', 'file'),
                (fix_path('./x/y/z'), 'z', 'folder'),
                (fix_path('./x/y'), 'y', 'folder'),
                (fix_path('./x'), 'x', 'folder'),
                ('.', testroot, 'folder'),
            ],
            [
                (relfile, 'test_eggs.py', 'file'),
                (fix_path('./x/y/z'), 'z', 'folder'),
                (fix_path('./x/y'), 'y', 'folder'),
                (fix_path('./x'), 'x', 'folder'),
                ('.', testroot, 'folder'),
            ],
        ]
        expected = [
            test._replace(id=_fix_nodeid(test.id),
                          parentid=_fix_nodeid(test.parentid))
            for test in alltests
        ]

        discovered = DiscoveredTests()

        for test, parents in zip(alltests, allparents):
            discovered.add_test(test, parents)
        tests = list(discovered)
        parents = discovered.parents

        self.maxDiff = None
        self.assertEqual(tests, expected)
        self.assertEqual(parents, [
            ParentInfo(
                id='.',
                kind='folder',
                name=testroot,
            ),
            ParentInfo(
                id='./x',
                kind='folder',
                name='x',
                root=testroot,
                relpath=fix_path('./x'),
                parentid='.',
            ),
            ParentInfo(
                id='./x/test_doctest.txt',
                kind='file',
                name='test_doctest.txt',
                root=testroot,
                relpath=fix_path(doctestfile),
                parentid='./x',
            ),
            ParentInfo(
                id='./x/y',
                kind='folder',
                name='y',
                root=testroot,
                relpath=fix_path('./x/y'),
                parentid='./x',
            ),
            ParentInfo(
                id='./x/y/z',
                kind='folder',
                name='z',
                root=testroot,
                relpath=fix_path('./x/y/z'),
                parentid='./x/y',
            ),
            ParentInfo(
                id='./x/y/z/test_eggs.py',
                kind='file',
                name='test_eggs.py',
                root=testroot,
                relpath=fix_relpath(relfile),
                parentid='./x/y/z',
            ),
        ])
Пример #6
0
    def test_multiroot(self):
        # the first root
        testroot1 = fix_path('/a/b/c')
        relfile1 = 'test_spam.py'
        alltests = [
            TestInfo(
                # missing "./":
                id=relfile1 + '::test_spam',
                name='test_spam',
                path=TestPath(
                    root=testroot1,
                    relfile=fix_relpath(relfile1),
                    func='test_spam',
                ),
                source='{}:{}'.format(relfile1, 10),
                markers=[],
                # missing "./":
                parentid=relfile1,
            ),
        ]
        allparents = [
            # missing "./":
            [
                (relfile1, 'test_spam.py', 'file'),
                ('.', testroot1, 'folder'),
            ],
        ]
        # the second root
        testroot2 = fix_path('/x/y/z')
        relfile2 = fix_path('w/test_eggs.py')
        alltests.extend([
            TestInfo(
                id=relfile2 + '::BasicTests::test_first',
                name='test_first',
                path=TestPath(
                    root=testroot2,
                    relfile=fix_relpath(relfile2),
                    func='BasicTests.test_first',
                ),
                source='{}:{}'.format(relfile2, 61),
                markers=[],
                parentid=relfile2 + '::BasicTests',
            ),
        ])
        allparents.extend([
            # missing "./", using pathsep:
            [
                (relfile2 + '::BasicTests', 'BasicTests', 'suite'),
                (relfile2, 'test_eggs.py', 'file'),
                (fix_path('./w'), 'w', 'folder'),
                ('.', testroot2, 'folder'),
            ],
        ])

        discovered = DiscoveredTests()
        for test, parents in zip(alltests, allparents):
            discovered.add_test(test, parents)
        tests = list(discovered)
        parents = discovered.parents

        self.maxDiff = None
        self.assertEqual(
            tests,
            [
                # the first root
                TestInfo(
                    id='./test_spam.py::test_spam',
                    name='test_spam',
                    path=TestPath(
                        root=testroot1,
                        relfile=fix_relpath(relfile1),
                        func='test_spam',
                    ),
                    source='{}:{}'.format(relfile1, 10),
                    markers=[],
                    parentid='./test_spam.py',
                ),
                # the secondroot
                TestInfo(
                    id='./w/test_eggs.py::BasicTests::test_first',
                    name='test_first',
                    path=TestPath(
                        root=testroot2,
                        relfile=fix_relpath(relfile2),
                        func='BasicTests.test_first',
                    ),
                    source='{}:{}'.format(relfile2, 61),
                    markers=[],
                    parentid='./w/test_eggs.py::BasicTests',
                ),
            ])
        self.assertEqual(
            parents,
            [
                # the first root
                ParentInfo(
                    id='.',
                    kind='folder',
                    name=testroot1,
                ),
                ParentInfo(
                    id='./test_spam.py',
                    kind='file',
                    name='test_spam.py',
                    root=testroot1,
                    relpath=fix_relpath(relfile1),
                    parentid='.',
                ),
                # the secondroot
                ParentInfo(
                    id='.',
                    kind='folder',
                    name=testroot2,
                ),
                ParentInfo(
                    id='./w',
                    kind='folder',
                    name='w',
                    root=testroot2,
                    relpath=fix_path('./w'),
                    parentid='.',
                ),
                ParentInfo(
                    id='./w/test_eggs.py',
                    kind='file',
                    name='test_eggs.py',
                    root=testroot2,
                    relpath=fix_relpath(relfile2),
                    parentid='./w',
                ),
                ParentInfo(
                    id='./w/test_eggs.py::BasicTests',
                    kind='suite',
                    name='BasicTests',
                    root=testroot2,
                    parentid='./w/test_eggs.py',
                ),
            ])
Пример #7
0
    def test_parents(self):
        testroot = fix_path('/a/b/c')
        relfile = fix_path('x/y/z/test_spam.py')
        tests = [
            TestInfo(
                # missing "./", using pathsep:
                id=relfile + '::test_each[10-10]',
                name='test_each[10-10]',
                path=TestPath(
                    root=testroot,
                    relfile=fix_relpath(relfile),
                    func='test_each',
                    sub=['[10-10]'],
                ),
                source='{}:{}'.format(relfile, 10),
                markers=None,
                # missing "./", using pathsep:
                parentid=relfile + '::test_each',
            ),
            TestInfo(
                # missing "./", using pathsep:
                id=relfile + '::All::BasicTests::test_first',
                name='test_first',
                path=TestPath(
                    root=testroot,
                    relfile=fix_relpath(relfile),
                    func='All.BasicTests.test_first',
                    sub=None,
                ),
                source='{}:{}'.format(relfile, 61),
                markers=None,
                # missing "./", using pathsep:
                parentid=relfile + '::All::BasicTests',
            ),
        ]
        allparents = [
            # missing "./", using pathsep:
            [
                (relfile + '::test_each', 'test_each', 'function'),
                (relfile, relfile, 'file'),
                ('.', testroot, 'folder'),
            ],
            # missing "./", using pathsep:
            [
                (relfile + '::All::BasicTests', 'BasicTests', 'suite'),
                (relfile + '::All', 'All', 'suite'),
                (relfile, 'test_spam.py', 'file'),
                (fix_path('x/y/z'), 'z', 'folder'),
                (fix_path('x/y'), 'y', 'folder'),
                (fix_path('./x'), 'x', 'folder'),
                ('.', testroot, 'folder'),
            ],
        ]
        discovered = DiscoveredTests()
        for test, parents in zip(tests, allparents):
            discovered.add_test(test, parents)

        parents = discovered.parents

        self.maxDiff = None
        self.assertEqual(parents, [
            ParentInfo(
                id='.',
                kind='folder',
                name=testroot,
            ),
            ParentInfo(
                id='./x',
                kind='folder',
                name='x',
                root=testroot,
                relpath=fix_path('./x'),
                parentid='.',
            ),
            ParentInfo(
                id='./x/y',
                kind='folder',
                name='y',
                root=testroot,
                relpath=fix_path('./x/y'),
                parentid='./x',
            ),
            ParentInfo(
                id='./x/y/z',
                kind='folder',
                name='z',
                root=testroot,
                relpath=fix_path('./x/y/z'),
                parentid='./x/y',
            ),
            ParentInfo(
                id='./x/y/z/test_spam.py',
                kind='file',
                name='test_spam.py',
                root=testroot,
                relpath=fix_relpath(relfile),
                parentid='./x/y/z',
            ),
            ParentInfo(
                id='./x/y/z/test_spam.py::All',
                kind='suite',
                name='All',
                root=testroot,
                parentid='./x/y/z/test_spam.py',
            ),
            ParentInfo(
                id='./x/y/z/test_spam.py::All::BasicTests',
                kind='suite',
                name='BasicTests',
                root=testroot,
                parentid='./x/y/z/test_spam.py::All',
            ),
            ParentInfo(
                id='./x/y/z/test_spam.py::test_each',
                kind='function',
                name='test_each',
                root=testroot,
                parentid='./x/y/z/test_spam.py',
            ),
        ])
Пример #8
0
    def test_basic(self):
        stub = StubSender()
        testroot = fix_path("/a/b/c")
        relfile = "test_spam.py"
        relpath = fix_relpath(relfile)
        tests = [
            TestInfo(
                id="test#1",
                name="test_spam",
                path=TestPath(
                    root=testroot,
                    relfile=relfile,
                    func="test_spam",
                ),
                source="{}:{}".format(relfile, 10),
                markers=[],
                parentid="file#1",
            ),
        ]
        parents = [
            ParentInfo(
                id="<root>",
                kind="folder",
                name=testroot,
            ),
            ParentInfo(
                id="file#1",
                kind="file",
                name=relfile,
                root=testroot,
                relpath=relpath,
                parentid="<root>",
            ),
        ]
        expected = [{
            "rootid":
            "<root>",
            "root":
            testroot,
            "parents": [
                {
                    "id": "file#1",
                    "kind": "file",
                    "name": relfile,
                    "relpath": relpath,
                    "parentid": "<root>",
                },
            ],
            "tests": [{
                "id": "test#1",
                "name": "test_spam",
                "source": "{}:{}".format(relfile, 10),
                "markers": [],
                "parentid": "file#1",
            }],
        }]

        report_discovered(tests, parents, _send=stub.send)

        self.maxDiff = None
        self.assertEqual(stub.calls, [
            ("send", (expected, ), None),
        ])
Пример #9
0
    def test_basic(self):
        stub = StubSender()
        testroot = fix_path('/a/b/c')
        relfile = 'test_spam.py'
        relpath = fix_relpath(relfile)
        tests = [
            TestInfo(
                id='test#1',
                name='test_spam',
                path=TestPath(
                    root=testroot,
                    relfile=relfile,
                    func='test_spam',
                ),
                source='{}:{}'.format(relfile, 10),
                markers=[],
                parentid='file#1',
            ),
        ]
        parents = [
            ParentInfo(
                id='<root>',
                kind='folder',
                name=testroot,
            ),
            ParentInfo(
                id='file#1',
                kind='file',
                name=relfile,
                root=testroot,
                relpath=relpath,
                parentid='<root>',
            ),
        ]
        expected = [{
            'rootid':
            '<root>',
            'root':
            testroot,
            'parents': [
                {
                    'id': 'file#1',
                    'kind': 'file',
                    'name': relfile,
                    'relpath': relpath,
                    'parentid': '<root>',
                },
            ],
            'tests': [{
                'id': 'test#1',
                'name': 'test_spam',
                'source': '{}:{}'.format(relfile, 10),
                'markers': [],
                'parentid': 'file#1',
            }],
        }]

        report_discovered(tests, parents, _send=stub.send)

        self.maxDiff = None
        self.assertEqual(stub.calls, [
            ('send', (expected, ), None),
        ])
Пример #10
0
    def test_nested_suite_simple(self):
        testroot = fix_path("/a/b/c")
        relfile = fix_path("./test_eggs.py")
        alltests = [
            SingleTestInfo(
                id=relfile + "::TestOuter::TestInner::test_spam",
                name="test_spam",
                path=SingleTestPath(
                    root=testroot,
                    relfile=relfile,
                    func="TestOuter.TestInner.test_spam",
                ),
                source="{}:{}".format(relfile, 10),
                markers=None,
                parentid=relfile + "::TestOuter::TestInner",
            ),
            SingleTestInfo(
                id=relfile + "::TestOuter::TestInner::test_eggs",
                name="test_eggs",
                path=SingleTestPath(
                    root=testroot,
                    relfile=relfile,
                    func="TestOuter.TestInner.test_eggs",
                ),
                source="{}:{}".format(relfile, 21),
                markers=None,
                parentid=relfile + "::TestOuter::TestInner",
            ),
        ]
        allparents = [
            [
                (relfile + "::TestOuter::TestInner", "TestInner", "suite"),
                (relfile + "::TestOuter", "TestOuter", "suite"),
                (relfile, "test_eggs.py", "file"),
                (".", testroot, "folder"),
            ],
            [
                (relfile + "::TestOuter::TestInner", "TestInner", "suite"),
                (relfile + "::TestOuter", "TestOuter", "suite"),
                (relfile, "test_eggs.py", "file"),
                (".", testroot, "folder"),
            ],
        ]
        expected = [
            test._replace(id=_fix_nodeid(test.id),
                          parentid=_fix_nodeid(test.parentid))
            for test in alltests
        ]

        discovered = DiscoveredTests()
        for test, parents in zip(alltests, allparents):
            discovered.add_test(test, parents)
        tests = list(discovered)
        parents = discovered.parents

        self.maxDiff = None
        self.assertEqual(tests, expected)
        self.assertEqual(
            parents,
            [
                ParentInfo(
                    id=".",
                    kind="folder",
                    name=testroot,
                ),
                ParentInfo(
                    id="./test_eggs.py",
                    kind="file",
                    name="test_eggs.py",
                    root=testroot,
                    relpath=fix_relpath(relfile),
                    parentid=".",
                ),
                ParentInfo(
                    id="./test_eggs.py::TestOuter",
                    kind="suite",
                    name="TestOuter",
                    root=testroot,
                    parentid="./test_eggs.py",
                ),
                ParentInfo(
                    id="./test_eggs.py::TestOuter::TestInner",
                    kind="suite",
                    name="TestInner",
                    root=testroot,
                    parentid="./test_eggs.py::TestOuter",
                ),
            ],
        )
Пример #11
0
    def test_doctest(self):
        testroot = fix_path("/a/b/c")
        doctestfile = fix_path("./x/test_doctest.txt")
        relfile = fix_path("./x/y/z/test_eggs.py")
        alltests = [
            SingleTestInfo(
                id=doctestfile + "::test_doctest.txt",
                name="test_doctest.txt",
                path=SingleTestPath(
                    root=testroot,
                    relfile=doctestfile,
                    func=None,
                ),
                source="{}:{}".format(doctestfile, 0),
                markers=[],
                parentid=doctestfile,
            ),
            # With --doctest-modules
            SingleTestInfo(
                id=relfile + "::test_eggs",
                name="test_eggs",
                path=SingleTestPath(
                    root=testroot,
                    relfile=relfile,
                    func=None,
                ),
                source="{}:{}".format(relfile, 0),
                markers=[],
                parentid=relfile,
            ),
            SingleTestInfo(
                id=relfile + "::test_eggs.TestSpam",
                name="test_eggs.TestSpam",
                path=SingleTestPath(
                    root=testroot,
                    relfile=relfile,
                    func=None,
                ),
                source="{}:{}".format(relfile, 12),
                markers=[],
                parentid=relfile,
            ),
            SingleTestInfo(
                id=relfile + "::test_eggs.TestSpam.TestEggs",
                name="test_eggs.TestSpam.TestEggs",
                path=SingleTestPath(
                    root=testroot,
                    relfile=relfile,
                    func=None,
                ),
                source="{}:{}".format(relfile, 27),
                markers=[],
                parentid=relfile,
            ),
        ]
        allparents = [
            [
                (doctestfile, "test_doctest.txt", "file"),
                (fix_path("./x"), "x", "folder"),
                (".", testroot, "folder"),
            ],
            [
                (relfile, "test_eggs.py", "file"),
                (fix_path("./x/y/z"), "z", "folder"),
                (fix_path("./x/y"), "y", "folder"),
                (fix_path("./x"), "x", "folder"),
                (".", testroot, "folder"),
            ],
            [
                (relfile, "test_eggs.py", "file"),
                (fix_path("./x/y/z"), "z", "folder"),
                (fix_path("./x/y"), "y", "folder"),
                (fix_path("./x"), "x", "folder"),
                (".", testroot, "folder"),
            ],
            [
                (relfile, "test_eggs.py", "file"),
                (fix_path("./x/y/z"), "z", "folder"),
                (fix_path("./x/y"), "y", "folder"),
                (fix_path("./x"), "x", "folder"),
                (".", testroot, "folder"),
            ],
        ]
        expected = [
            test._replace(id=_fix_nodeid(test.id),
                          parentid=_fix_nodeid(test.parentid))
            for test in alltests
        ]

        discovered = DiscoveredTests()

        for test, parents in zip(alltests, allparents):
            discovered.add_test(test, parents)
        tests = list(discovered)
        parents = discovered.parents

        self.maxDiff = None
        self.assertEqual(tests, expected)
        self.assertEqual(
            parents,
            [
                ParentInfo(
                    id=".",
                    kind="folder",
                    name=testroot,
                ),
                ParentInfo(
                    id="./x",
                    kind="folder",
                    name="x",
                    root=testroot,
                    relpath=fix_path("./x"),
                    parentid=".",
                ),
                ParentInfo(
                    id="./x/test_doctest.txt",
                    kind="file",
                    name="test_doctest.txt",
                    root=testroot,
                    relpath=fix_path(doctestfile),
                    parentid="./x",
                ),
                ParentInfo(
                    id="./x/y",
                    kind="folder",
                    name="y",
                    root=testroot,
                    relpath=fix_path("./x/y"),
                    parentid="./x",
                ),
                ParentInfo(
                    id="./x/y/z",
                    kind="folder",
                    name="z",
                    root=testroot,
                    relpath=fix_path("./x/y/z"),
                    parentid="./x/y",
                ),
                ParentInfo(
                    id="./x/y/z/test_eggs.py",
                    kind="file",
                    name="test_eggs.py",
                    root=testroot,
                    relpath=fix_relpath(relfile),
                    parentid="./x/y/z",
                ),
            ],
        )
Пример #12
0
    def test_multiroot(self):
        # the first root
        testroot1 = fix_path("/a/b/c")
        relfile1 = "test_spam.py"
        alltests = [
            SingleTestInfo(
                # missing "./":
                id=relfile1 + "::test_spam",
                name="test_spam",
                path=SingleTestPath(
                    root=testroot1,
                    relfile=fix_relpath(relfile1),
                    func="test_spam",
                ),
                source="{}:{}".format(relfile1, 10),
                markers=[],
                # missing "./":
                parentid=relfile1,
            ),
        ]
        allparents = [
            # missing "./":
            [
                (relfile1, "test_spam.py", "file"),
                (".", testroot1, "folder"),
            ],
        ]
        # the second root
        testroot2 = fix_path("/x/y/z")
        relfile2 = fix_path("w/test_eggs.py")
        alltests.extend([
            SingleTestInfo(
                id=relfile2 + "::BasicTests::test_first",
                name="test_first",
                path=SingleTestPath(
                    root=testroot2,
                    relfile=fix_relpath(relfile2),
                    func="BasicTests.test_first",
                ),
                source="{}:{}".format(relfile2, 61),
                markers=[],
                parentid=relfile2 + "::BasicTests",
            ),
        ])
        allparents.extend([
            # missing "./", using pathsep:
            [
                (relfile2 + "::BasicTests", "BasicTests", "suite"),
                (relfile2, "test_eggs.py", "file"),
                (fix_path("./w"), "w", "folder"),
                (".", testroot2, "folder"),
            ],
        ])

        discovered = DiscoveredTests()
        for test, parents in zip(alltests, allparents):
            discovered.add_test(test, parents)
        tests = list(discovered)
        parents = discovered.parents

        self.maxDiff = None
        self.assertEqual(
            tests,
            [
                # the first root
                SingleTestInfo(
                    id="./test_spam.py::test_spam",
                    name="test_spam",
                    path=SingleTestPath(
                        root=testroot1,
                        relfile=fix_relpath(relfile1),
                        func="test_spam",
                    ),
                    source="{}:{}".format(relfile1, 10),
                    markers=[],
                    parentid="./test_spam.py",
                ),
                # the secondroot
                SingleTestInfo(
                    id="./w/test_eggs.py::BasicTests::test_first",
                    name="test_first",
                    path=SingleTestPath(
                        root=testroot2,
                        relfile=fix_relpath(relfile2),
                        func="BasicTests.test_first",
                    ),
                    source="{}:{}".format(relfile2, 61),
                    markers=[],
                    parentid="./w/test_eggs.py::BasicTests",
                ),
            ],
        )
        self.assertEqual(
            parents,
            [
                # the first root
                ParentInfo(
                    id=".",
                    kind="folder",
                    name=testroot1,
                ),
                ParentInfo(
                    id="./test_spam.py",
                    kind="file",
                    name="test_spam.py",
                    root=testroot1,
                    relpath=fix_relpath(relfile1),
                    parentid=".",
                ),
                # the secondroot
                ParentInfo(
                    id=".",
                    kind="folder",
                    name=testroot2,
                ),
                ParentInfo(
                    id="./w",
                    kind="folder",
                    name="w",
                    root=testroot2,
                    relpath=fix_path("./w"),
                    parentid=".",
                ),
                ParentInfo(
                    id="./w/test_eggs.py",
                    kind="file",
                    name="test_eggs.py",
                    root=testroot2,
                    relpath=fix_relpath(relfile2),
                    parentid="./w",
                ),
                ParentInfo(
                    id="./w/test_eggs.py::BasicTests",
                    kind="suite",
                    name="BasicTests",
                    root=testroot2,
                    parentid="./w/test_eggs.py",
                ),
            ],
        )
Пример #13
0
    def test_parents(self):
        testroot = fix_path("/a/b/c")
        relfile = fix_path("x/y/z/test_spam.py")
        tests = [
            SingleTestInfo(
                # missing "./", using pathsep:
                id=relfile + "::test_each[10-10]",
                name="test_each[10-10]",
                path=SingleTestPath(
                    root=testroot,
                    relfile=fix_relpath(relfile),
                    func="test_each",
                    sub=["[10-10]"],
                ),
                source="{}:{}".format(relfile, 10),
                markers=None,
                # missing "./", using pathsep:
                parentid=relfile + "::test_each",
            ),
            SingleTestInfo(
                # missing "./", using pathsep:
                id=relfile + "::All::BasicTests::test_first",
                name="test_first",
                path=SingleTestPath(
                    root=testroot,
                    relfile=fix_relpath(relfile),
                    func="All.BasicTests.test_first",
                    sub=None,
                ),
                source="{}:{}".format(relfile, 61),
                markers=None,
                # missing "./", using pathsep:
                parentid=relfile + "::All::BasicTests",
            ),
        ]
        allparents = [
            # missing "./", using pathsep:
            [
                (relfile + "::test_each", "test_each", "function"),
                (relfile, relfile, "file"),
                (".", testroot, "folder"),
            ],
            # missing "./", using pathsep:
            [
                (relfile + "::All::BasicTests", "BasicTests", "suite"),
                (relfile + "::All", "All", "suite"),
                (relfile, "test_spam.py", "file"),
                (fix_path("x/y/z"), "z", "folder"),
                (fix_path("x/y"), "y", "folder"),
                (fix_path("./x"), "x", "folder"),
                (".", testroot, "folder"),
            ],
        ]
        discovered = DiscoveredTests()
        for test, parents in zip(tests, allparents):
            discovered.add_test(test, parents)

        parents = discovered.parents

        self.maxDiff = None
        self.assertEqual(
            parents,
            [
                ParentInfo(
                    id=".",
                    kind="folder",
                    name=testroot,
                ),
                ParentInfo(
                    id="./x",
                    kind="folder",
                    name="x",
                    root=testroot,
                    relpath=fix_path("./x"),
                    parentid=".",
                ),
                ParentInfo(
                    id="./x/y",
                    kind="folder",
                    name="y",
                    root=testroot,
                    relpath=fix_path("./x/y"),
                    parentid="./x",
                ),
                ParentInfo(
                    id="./x/y/z",
                    kind="folder",
                    name="z",
                    root=testroot,
                    relpath=fix_path("./x/y/z"),
                    parentid="./x/y",
                ),
                ParentInfo(
                    id="./x/y/z/test_spam.py",
                    kind="file",
                    name="test_spam.py",
                    root=testroot,
                    relpath=fix_relpath(relfile),
                    parentid="./x/y/z",
                ),
                ParentInfo(
                    id="./x/y/z/test_spam.py::All",
                    kind="suite",
                    name="All",
                    root=testroot,
                    parentid="./x/y/z/test_spam.py",
                ),
                ParentInfo(
                    id="./x/y/z/test_spam.py::All::BasicTests",
                    kind="suite",
                    name="BasicTests",
                    root=testroot,
                    parentid="./x/y/z/test_spam.py::All",
                ),
                ParentInfo(
                    id="./x/y/z/test_spam.py::test_each",
                    kind="function",
                    name="test_each",
                    root=testroot,
                    parentid="./x/y/z/test_spam.py",
                ),
            ],
        )
Пример #14
0
    def test_doctest(self):
        stub = Stub()
        discovered = StubDiscoveredTests(stub)
        session = StubPytestSession(stub)
        testroot = fix_path('/a/b/c')
        doctestfile = fix_path('x/test_doctest.txt')
        relfile = fix_path('x/y/z/test_eggs.py')
        session.items = [
            StubDoctestItem(
                stub,
                nodeid=doctestfile + '::test_doctest.txt',
                name='test_doctest.txt',
                location=(doctestfile, 0, '[doctest] test_doctest.txt'),
                fspath=PATH_JOIN(testroot, doctestfile),
                ),
            # With --doctest-modules
            StubDoctestItem(
                stub,
                nodeid=relfile + '::test_eggs',
                name='test_eggs',
                location=(relfile, 0, '[doctest] test_eggs'),
                fspath=PATH_JOIN(testroot, relfile),
                ),
            StubDoctestItem(
                stub,
                nodeid=relfile + '::test_eggs.TestSpam',
                name='test_eggs.TestSpam',
                location=(relfile, 12, '[doctest] test_eggs.TestSpam'),
                fspath=PATH_JOIN(testroot, relfile),
                ),
            StubDoctestItem(
                stub,
                nodeid=relfile + '::test_eggs.TestSpam.TestEggs',
                name='test_eggs.TestSpam.TestEggs',
                location=(relfile, 27, '[doctest] test_eggs.TestSpam.TestEggs'),
                fspath=PATH_JOIN(testroot, relfile),
                ),
            ]
        collector = TestCollector(tests=discovered)

        collector.pytest_collection_finish(session)

        self.maxDiff = None
        self.assertEqual(stub.calls, [
            ('discovered.reset', None, None),
            ('discovered.add_test', None, dict(
                parents=[
                    ('./x/test_doctest.txt', 'test_doctest.txt', 'file'),
                    ('./x', 'x', 'folder'),
                    ('.', testroot, 'folder'),
                    ],
                test=TestInfo(
                    id='./x/test_doctest.txt::test_doctest.txt',
                    name='test_doctest.txt',
                    path=TestPath(
                        root=testroot,
                        relfile=fix_relpath(doctestfile),
                        func=None,
                        ),
                    source='{}:{}'.format(fix_relpath(doctestfile), 1),
                    markers=[],
                    parentid='./x/test_doctest.txt',
                    ),
                )),
            ('discovered.add_test', None, dict(
                parents=[
                    ('./x/y/z/test_eggs.py', 'test_eggs.py', 'file'),
                    ('./x/y/z', 'z', 'folder'),
                    ('./x/y', 'y', 'folder'),
                    ('./x', 'x', 'folder'),
                    ('.', testroot, 'folder'),
                    ],
                test=TestInfo(
                    id='./x/y/z/test_eggs.py::test_eggs',
                    name='test_eggs',
                    path=TestPath(
                        root=testroot,
                        relfile=fix_relpath(relfile),
                        func=None,
                        ),
                    source='{}:{}'.format(fix_relpath(relfile), 1),
                    markers=[],
                    parentid='./x/y/z/test_eggs.py',
                    ),
                )),
            ('discovered.add_test', None, dict(
                parents=[
                    ('./x/y/z/test_eggs.py', 'test_eggs.py', 'file'),
                    ('./x/y/z', 'z', 'folder'),
                    ('./x/y', 'y', 'folder'),
                    ('./x', 'x', 'folder'),
                    ('.', testroot, 'folder'),
                    ],
                test=TestInfo(
                    id='./x/y/z/test_eggs.py::test_eggs.TestSpam',
                    name='test_eggs.TestSpam',
                    path=TestPath(
                        root=testroot,
                        relfile=fix_relpath(relfile),
                        func=None,
                        ),
                    source='{}:{}'.format(fix_relpath(relfile), 13),
                    markers=[],
                    parentid='./x/y/z/test_eggs.py',
                    ),
                )),
            ('discovered.add_test', None, dict(
                parents=[
                    ('./x/y/z/test_eggs.py', 'test_eggs.py', 'file'),
                    ('./x/y/z', 'z', 'folder'),
                    ('./x/y', 'y', 'folder'),
                    ('./x', 'x', 'folder'),
                    ('.', testroot, 'folder'),
                    ],
                test=TestInfo(
                    id='./x/y/z/test_eggs.py::test_eggs.TestSpam.TestEggs',
                    name='test_eggs.TestSpam.TestEggs',
                    path=TestPath(
                        root=testroot,
                        relfile=fix_relpath(relfile),
                        func=None,
                        ),
                    source='{}:{}'.format(fix_relpath(relfile), 28),
                    markers=[],
                    parentid='./x/y/z/test_eggs.py',
                    ),
                )),
            ])
Пример #15
0
    def test_modifyitems(self):
        stub = Stub()
        discovered = StubDiscoveredTests(stub)
        session = StubPytestSession(stub)
        config = StubPytestConfig(stub)
        collector = TestCollector(tests=discovered)

        testroot = fix_path('/a/b/c')
        relfile1 = fix_path('./test_spam.py')
        relfile2 = fix_path('x/y/z/test_eggs.py')

        collector.pytest_collection_modifyitems(session, config, [
            StubFunctionItem(
                stub,
                nodeid='test_spam.py::SpamTests::test_one',
                name='test_one',
                location=('test_spam.py', 12, 'SpamTests.test_one'),
                fspath=PATH_JOIN(testroot, 'test_spam.py'),
                function=FakeFunc('test_one'),
                ),
            StubFunctionItem(
                stub,
                nodeid='test_spam.py::SpamTests::test_other',
                name='test_other',
                location=('test_spam.py', 19, 'SpamTests.test_other'),
                fspath=PATH_JOIN(testroot, 'test_spam.py'),
                function=FakeFunc('test_other'),
                ),
            StubFunctionItem(
                stub,
                nodeid='test_spam.py::test_all',
                name='test_all',
                location=('test_spam.py', 144, 'test_all'),
                fspath=PATH_JOIN(testroot, 'test_spam.py'),
                function=FakeFunc('test_all'),
                ),
            StubFunctionItem(
                stub,
                nodeid='test_spam.py::test_each[10-10]',
                name='test_each[10-10]',
                location=('test_spam.py', 273, 'test_each[10-10]'),
                fspath=PATH_JOIN(testroot, 'test_spam.py'),
                function=FakeFunc('test_each'),
                ),
            StubFunctionItem(
                stub,
                nodeid=relfile2 + '::All::BasicTests::test_first',
                name='test_first',
                location=(relfile2, 31, 'All.BasicTests.test_first'),
                fspath=PATH_JOIN(testroot, relfile2),
                function=FakeFunc('test_first'),
                ),
            StubFunctionItem(
                stub,
                nodeid=relfile2 + '::All::BasicTests::test_each[1+2-3]',
                name='test_each[1+2-3]',
                location=(relfile2, 62, 'All.BasicTests.test_each[1+2-3]'),
                fspath=PATH_JOIN(testroot, relfile2),
                function=FakeFunc('test_each'),
                own_markers=[FakeMarker(v) for v in [
                    # supported
                    'skip', 'skipif', 'xfail',
                    # duplicate
                    'skip',
                    # ignored (pytest-supported)
                    'parameterize', 'usefixtures', 'filterwarnings',
                    # ignored (custom)
                    'timeout',
                    ]],
                ),
            ])

        self.maxDiff = None
        self.assertEqual(stub.calls, [
            ('discovered.reset', None, None),
            ('discovered.add_test', None, dict(
                parents=[
                    ('./test_spam.py::SpamTests', 'SpamTests', 'suite'),
                    ('./test_spam.py', 'test_spam.py', 'file'),
                    ('.', testroot, 'folder'),
                    ],
                test=TestInfo(
                    id='./test_spam.py::SpamTests::test_one',
                    name='test_one',
                    path=TestPath(
                        root=testroot,
                        relfile=relfile1,
                        func='SpamTests.test_one',
                        sub=None,
                        ),
                    source='{}:{}'.format(relfile1, 13),
                    markers=None,
                    parentid='./test_spam.py::SpamTests',
                    ),
                )),
            ('discovered.add_test', None, dict(
                parents=[
                    ('./test_spam.py::SpamTests', 'SpamTests', 'suite'),
                    ('./test_spam.py', 'test_spam.py', 'file'),
                    ('.', testroot, 'folder'),
                    ],
                test=TestInfo(
                    id='./test_spam.py::SpamTests::test_other',
                    name='test_other',
                    path=TestPath(
                        root=testroot,
                        relfile=relfile1,
                        func='SpamTests.test_other',
                        sub=None,
                        ),
                    source='{}:{}'.format(relfile1, 20),
                    markers=None,
                    parentid='./test_spam.py::SpamTests',
                    ),
                )),
            ('discovered.add_test', None, dict(
                parents=[
                    ('./test_spam.py', 'test_spam.py', 'file'),
                    ('.', testroot, 'folder'),
                    ],
                test=TestInfo(
                    id='./test_spam.py::test_all',
                    name='test_all',
                    path=TestPath(
                        root=testroot,
                        relfile=relfile1,
                        func='test_all',
                        sub=None,
                        ),
                    source='{}:{}'.format(relfile1, 145),
                    markers=None,
                    parentid='./test_spam.py',
                    ),
                )),
            ('discovered.add_test', None, dict(
                parents=[
                    ('./test_spam.py::test_each', 'test_each', 'function'),
                    ('./test_spam.py', 'test_spam.py', 'file'),
                    ('.', testroot, 'folder'),
                    ],
                test=TestInfo(
                    id='./test_spam.py::test_each[10-10]',
                    name='test_each[10-10]',
                    path=TestPath(
                        root=testroot,
                        relfile=relfile1,
                        func='test_each',
                        sub=['[10-10]'],
                        ),
                    source='{}:{}'.format(relfile1, 274),
                    markers=None,
                    parentid='./test_spam.py::test_each',
                    ),
                )),
            ('discovered.add_test', None, dict(
                parents=[
                    ('./x/y/z/test_eggs.py::All::BasicTests', 'BasicTests', 'suite'),
                    ('./x/y/z/test_eggs.py::All', 'All', 'suite'),
                    ('./x/y/z/test_eggs.py', 'test_eggs.py', 'file'),
                    ('./x/y/z', 'z', 'folder'),
                    ('./x/y', 'y', 'folder'),
                    ('./x', 'x', 'folder'),
                    ('.', testroot, 'folder'),
                    ],
                test=TestInfo(
                    id='./x/y/z/test_eggs.py::All::BasicTests::test_first',
                    name='test_first',
                    path=TestPath(
                        root=testroot,
                        relfile=fix_relpath(relfile2),
                        func='All.BasicTests.test_first',
                        sub=None,
                        ),
                    source='{}:{}'.format(fix_relpath(relfile2), 32),
                    markers=None,
                    parentid='./x/y/z/test_eggs.py::All::BasicTests',
                    ),
                )),
            ('discovered.add_test', None, dict(
                parents=[
                    ('./x/y/z/test_eggs.py::All::BasicTests::test_each', 'test_each', 'function'),
                    ('./x/y/z/test_eggs.py::All::BasicTests', 'BasicTests', 'suite'),
                    ('./x/y/z/test_eggs.py::All', 'All', 'suite'),
                    ('./x/y/z/test_eggs.py', 'test_eggs.py', 'file'),
                    ('./x/y/z', 'z', 'folder'),
                    ('./x/y', 'y', 'folder'),
                    ('./x', 'x', 'folder'),
                    ('.', testroot, 'folder'),
                    ],
                test=TestInfo(
                    id='./x/y/z/test_eggs.py::All::BasicTests::test_each[1+2-3]',
                    name='test_each[1+2-3]',
                    path=TestPath(
                        root=testroot,
                        relfile=fix_relpath(relfile2),
                        func='All.BasicTests.test_each',
                        sub=['[1+2-3]'],
                        ),
                    source='{}:{}'.format(fix_relpath(relfile2), 63),
                    markers=['expected-failure', 'skip', 'skip-if'],
                    parentid='./x/y/z/test_eggs.py::All::BasicTests::test_each',
                    ),
                )),
            ])