从 unittest.TestCase 切换到 tf.test.TestCase 后的幻像测试 [英] Phantom tests after switching from unittest.TestCase to tf.test.TestCase

查看:34
本文介绍了从 unittest.TestCase 切换到 tf.test.TestCase 后的幻像测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码:

class BoxListOpsTest(unittest.TestCase):"""测试常见的边界框操作."""def test_area(self):角 = tf.constant([[0.0, 0.0, 10.0, 20.0], [1.0, 2.0, 3.0, 4.0]])exp_output = [200.0, 4.0]盒子 = box_list.BoxList(角落)区域 = box_list_ops.area(boxes)使用 tf.Session() 作为 sess:area_output = sess.run(areas)np.testing.assert_allclose(areas_output, exp_output)如果 __name__ == '__main__':单元测试.main()

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

<预><代码>.----------------------------------------------------------------------在 0.471 秒内运行 1 次测试好的

然而,切换到tf.test.TestCase:

class BoxListOpsTest(tf.test.TestCase):"""测试常见的边界框操作."""def test_area(self):角 = tf.constant([[0.0, 0.0, 10.0, 20.0], [1.0, 2.0, 3.0, 4.0]])exp_output = [200.0, 4.0]盒子 = box_list.BoxList(角落)区域 = box_list_ops.area(boxes)# 使用 self.session() 作为 sess:使用 tf.Session() 作为 sess:area_output = sess.run(areas)np.testing.assert_allclose(areas_output, exp_output)如果 __name__ == '__main__':tf.test.main()

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

.s----------------------------------------------------------------------在 0.524 秒内运行 2 个测试好的(跳过=1)

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

我使用的是 TensorFlow 1.13.

解决方案

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

def test_session(self,图=无,配置=无,use_gpu=False,force_gpu=假):如果 self.id().endswith(".test_session"):self.skipTest("不是测试.")

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

<代码>...test_session (BoxListOpsTest)请改用 cached_session.(已弃用)...跳过不是测试".

尽管 test_session 自 1.11 起已弃用,应替换为 cached_session (相关提交),截至目前,它还没有计划在 2.0 中删除.为了摆脱它,您可以对收集的测试应用自定义过滤器.

单元测试

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

test_cases = (BoxListOpsTest, )def load_tests(加载器,测试,模式):套件 = unittest.TestSuite()对于 test_cases 中的 test_class:测试 = loader.loadTestsFromTestCase(test_class)filtered_tests = [t for t in tests if not t.id().endswith('.test_session')]suite.addTests(filtered_tests)回程套房

pytest

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

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

The following code:

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()

Is interpreted as a test case with a single test:

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

OK

However, switching to 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()

introduces some second test, which is skipped:

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

OK (skipped=1)

What is the origin of the second test and should I worry about it?

I am using TensorFlow 1.13.

解决方案

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']

这篇关于从 unittest.TestCase 切换到 tf.test.TestCase 后的幻像测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆