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

问题描述 投票:1回答:1

以下代码:

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。

python unit-testing tensorflow python-unittest
1个回答
1
投票

这是tf.test.TestCase.test_session方法。由于不幸的命名,unittest认为test_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.")

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

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

虽然test_session自1.11以来已被弃用,应该用cached_sessionrelated 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

pytest_collection_modifyitems中添加自定义conftest.py钩子:

def pytest_collection_modifyitems(session, config, items):
    items[:] = [item for item in items if item.name != 'test_session']
© www.soinside.com 2019 - 2024. All rights reserved.