Phantom tests after switching from unittest.TestCase to tf.test.TestCase

后端 未结 1 1457
面向向阳花
面向向阳花 2021-01-25 04:53

The following code:

class BoxListOpsTest(unittest.TestCase):                                                                                                              


        
相关标签:
1条回答
  • 2021-01-25 05:23

    It's the tf.test.TestCase.test_session method. Due to the unlucky naming, unittest considers test_session method to be a test and adds it to the test suite. To prevent running test_session as a test, Tensorflow has to skip it internally, so it results in a "skipped" test:

    def test_session(self,
                     graph=None,
                     config=None,
                     use_gpu=False,
                     force_gpu=False):
        if self.id().endswith(".test_session"):
            self.skipTest("Not a test.")
    

    Verify the skipped test is the test_session by running your test with a --verbose flag. You should see an output similar to this:

    ...
    test_session (BoxListOpsTest)
    Use cached_session instead. (deprecated) ... skipped 'Not a test.'
    

    Although the test_session is deprecated since 1.11 and should be replaced with cached_session (related commit), as of now, it's not scheduled for removal in 2.0 yet. In order to get rid of it, you can apply custom filter on collected tests.

    unittest

    You can define a custom load_tests function:

    test_cases = (BoxListOpsTest, )
    
    def load_tests(loader, tests, pattern):
        suite = unittest.TestSuite()
        for test_class in test_cases:
            tests = loader.loadTestsFromTestCase(test_class)
            filtered_tests = [t for t in tests if not t.id().endswith('.test_session')]
            suite.addTests(filtered_tests)
        return suite
    

    pytest

    Add a custom pytest_collection_modifyitems hook in your conftest.py:

    def pytest_collection_modifyitems(session, config, items):
        items[:] = [item for item in items if item.name != 'test_session']
    
    0 讨论(0)
提交回复
热议问题