从unittest.TestCase切换到tf.test.TestCase后的幻像测试

Dmy*_*pko 4 python unit-testing python-unittest tensorflow

下面的代码:

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()
Run Code Online (Sandbox Code Playgroud)

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

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

OK
Run Code Online (Sandbox Code Playgroud)

但是,切换到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()
Run Code Online (Sandbox Code Playgroud)

引入了第二个测试,该测试被跳过:

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

OK (skipped=1)
Run Code Online (Sandbox Code Playgroud)

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

我使用的是 TensorFlow 1.13。

hoe*_*ing 6

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

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.")
Run Code Online (Sandbox Code Playgroud)

test_session通过使用标志运行测试来验证跳过的测试--verbose。您应该看到与此类似的输出:

...
test_session (BoxListOpsTest)
Use cached_session instead. (deprecated) ... skipped 'Not a test.'
Run Code Online (Sandbox Code Playgroud)

尽管test_session自 1.11 起已弃用并应替换为cached_session相关提交),但截至目前,尚未计划在 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
Run Code Online (Sandbox Code Playgroud)

pytest

pytest_collection_modifyitems在您的中添加自定义挂钩conftest.py

def pytest_collection_modifyitems(session, config, items):
    items[:] = [item for item in items if item.name != 'test_session']
Run Code Online (Sandbox Code Playgroud)