Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 544
Dmytro Prylipko Avatar asked Oct 26 '25 08:10

Dmytro Prylipko


1 Answers

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']
like image 183
hoefling Avatar answered Oct 29 '25 03:10

hoefling