简体   繁体   English

同时使用 pytest 和 tf.test.TestCase 的问题

[英]Problems with using pytest and tf.test.TestCase simultaneously

I have a simple unit test where I check if I can instantiate my Tensorflow class with slightly different parameters.我有一个简单的单元测试,我在其中检查是否可以使用稍微不同的参数实例化我的 Tensorflow 类。 This seems like a great use case for @pytest.mark.parametrize .这似乎是@pytest.mark.parametrize一个很好的用例。

However, I've discovered that parametrize is ignored if I my unit tests are methods of a tf.test.TestCase .但是,我发现如果我的单元测试是tf.test.TestCase方法,则parametrize被忽略。

For example, when I run pytest on the following code:例如,当我对以下代码运行pytest时:

class TestBasicRewardNet(tf.test.TestCase):                                                                                                                          
    @pytest.mark.parametrize("env", ['FrozenLake-v0', 'CartPole-v1',                                                                                               
        'CarRacing-v0', 'LunarLander-v2'])                                                                                                                           
    def test_init_no_crash(self, env):                                                                                                                               
        for i in range(3):                                                                                                                                    
            x = BasicRewardNet(env)  

I get the error TypeError: test_init_no_crash() missing 1 required positional argument: 'env' .我收到错误TypeError: test_init_no_crash() missing 1 required positional argument: 'env'

To fix this issue, I tried just getting rid of the class wrapper, but that makes me miss out on some automatic Tensorflow test initialization.为了解决这个问题,我尝试去掉类包装器,但这让我错过了一些自动 Tensorflow 测试初始化​​。 In particular, now every BasicRewardNet is built in the same TensorFlow graph, and so I need to do something like add a variable scope to avoid conflicts.特别是,现在每个BasicRewardNet都构建在同一个 TensorFlow 图中,所以我需要做一些事情,比如添加一个变量范围以避免冲突。 Adding in this variable scope seems hacky.添加这个变量范围似乎很麻烦。

@pytest.mark.parametrize("env", ['FrozenLake-v0', 'CartPole-v1',                                                                                               
     'CarRacing-v0', 'LunarLander-v2'])  
def test_init_no_crash(env):                                                                                                                                         
    for i in range(3):                                                                                                                                               
        with tf.variable_scope(env+str(i)):                                                                                                                          
            x = BasicRewardNet(env)   

I'm wondering if anyone knows a way I can cleanly get the best of both worlds?我想知道是否有人知道我可以干净利落地获得两全其美的方法? I'd like to be able to use parametrize and get the automatic Tensorflow initialization of tf.test.TestCase at the same time.我希望能够使用parametrize ,并得到自动Tensorflow初始化tf.test.TestCase在同一时间。

As mentionned in the comments by hoefling , it can be solved using tf.test.TestCase.subTest .正如hoefling在评论中提到的,它可以使用tf.test.TestCase.subTest解决。

class TestBasicRewardNet(tf.test.TestCase):

    @staticmethod
    def my_sub_test(env):
        for i in range(3):                                                                                                                                               
            with tf.variable_scope(env+str(i)):                                                                                                                          
                x = BasicRewardNet(env)

    def test_init_no_crash(env):
        for env in ['FrozenLake-v0', 'CartPole-v1','CarRacing-v0', 'LunarLander-v2']:
            with self.subTest(env):                                                                                                                                                                                                                    
                 self.my_sub_test(env)

To be able to use the subTest features when running with pytest , you should add pytest-subtests in the requirements , otherwise you won't have them!为了能够在与pytest运行时使用subTest功能,您应该在需求中添加pytest-subtests ,否则您将没有它们!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM