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

时间:2019-03-29 12:12:24

标签: python unit-testing tensorflow python-unittest

以下代码:

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。

1 个答案:

答案 0 :(得分: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

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

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