从 unittest.TestCase 切换到 tf.test.TestCase 后的虚拟测试

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

以下代码:

class BoxListOpsTest(unittest.TestCase):                                                                                                                                                                                                                              
    """Tests for common bounding box operations."""                                                                                                                                                                                                                   

    def test_area(self):                                                                                                                                                                                                                                              
        corners = tf.constant([[0.0, 0.0, 10.0, 20.0], [1.0, 2.0, 3.0, 4.0]])                                                                                                                                                                                         
        exp_output = [200.0, 4.0]                                                                                                                                                                                                                                     
        boxes = box_list.BoxList(corners)                                                                                                                                                                                                                             
        areas = box_list_ops.area(boxes)                                                                                                                                                                                                                              

        with tf.Session() as sess:                                                                                                                                                                                                                                    
            areas_output = sess.run(areas)                                                                                                                                                                                                                            
            np.testing.assert_allclose(areas_output, exp_output)                                                                                                                                                                                                      


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

被解释为一个带有单个测试的测试用例:

.
----------------------------------------------------------------------
Ran 1 test in 0.471s

OK

但是,切换到 tf.test.TestCase:

class BoxListOpsTest(tf.test.TestCase):                                                                                                                                                                                                                               
    """Tests for common bounding box operations."""                                                                                                                                                                                                                   

    def test_area(self):                                                                                                                                                                                                                                              
        corners = tf.constant([[0.0, 0.0, 10.0, 20.0], [1.0, 2.0, 3.0, 4.0]])                                                                                                                                                                                         
        exp_output = [200.0, 4.0]                                                                                                                                                                                                                                     
        boxes = box_list.BoxList(corners)                                                                                                                                                                                                                             
        areas = box_list_ops.area(boxes)                                                                                                                                                                                                                              
        # with self.session() as sess:                                                                                                                                                                                                                                
        with tf.Session() as sess:                                                                                                                                                                                                                                    
            areas_output = sess.run(areas)                                                                                                                                                                                                                            
            np.testing.assert_allclose(areas_output, exp_output)                                                                                                                                                                                                      


if __name__ == '__main__':                                                                                                                                                                                                                                            
    tf.test.main()

介绍一些第二个测试,跳过:

.s
----------------------------------------------------------------------
Ran 2 tests in 0.524s

OK (skipped=1)

第二次测试的来源是什么,我应该担心吗?

我正在使用 TensorFlow 1.13。

这是tf.test.TestCase.test_session方法。由于命名不吉利,unittesttest_session 方法视为测试并将其添加到测试套件中。为了防止 运行 test_session 作为测试,Tensorflow 必须在内部跳过它,因此它会导致 "skipped" 测试:

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.")

通过 运行 带有 --verbose 标志的测试验证跳过的测试是 test_session。您应该会看到类似这样的输出:

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

虽然 test_session 自 1.11 起已弃用,应替换为 cached_session (related commit),但截至目前,尚未计划在 2.0 中删除它。为了摆脱它,您可以对收集的测试应用自定义过滤器。

unittest

您可以定义自定义 load_tests 函数:

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

在您的 conftest.py:

中添加自定义 pytest_collection_modifyitems 挂钩
def pytest_collection_modifyitems(session, config, items):
    items[:] = [item for item in items if item.name != 'test_session']