【问题标题】:How to run the methods in order for each input given by pytest parametrize如何为 pytest 参数化给出的每个输入按顺序运行方法
【发布时间】:2020-02-23 23:33:19
【问题描述】:

我有两种方法edit_configget_config,我通过pytest 参数化传递参数。问题是首先编辑配置将对所有输入执行,然后执行 get_config。但我想先为每个输入运行 edit_config 然后 get_config 像一个循环一样。我怎样才能与 pytest 参数化一起做到这一点?

@pytest.mark.parametrize("interval, lacp_mode", [("fast", "active"), ("fast", "passive"), ("slow", "active"),
                                                 ("slow", "passive")])
class Example:
    def test_edit_config(self, interval, mode):
        pass
    def test_get_config(self, interval, mode):
        pass

实际 - 首先为所有参数化输入运行 edit-config,然后运行 ​​get-config

预期 - 应该运行 edit-config 然后 get-config 像每个输入的循环一样

【问题讨论】:

    标签: python pytest


    【解决方案1】:

    pytest 创建测试并按字母顺序运行它们(默认行为),因此四个test_edit_config 测试将在test_get_config 测试之前运行。

    您可以创建一个测试来调用其他测试的功能

    @pytest.mark.parametrize("interval, lacp_mode", [("fast", "active"), ("fast", "passive"), ("slow", "active"), ("slow", "passive")])
    class TestExample:
    
        def test_config(self, interval, lacp_mode):
            self.__edit_config(interval, lacp_mode)
            self.__get_config(interval, lacp_mode)
    
        def __edit_config(self, interval, lacp_mode):
            pass
    
        def __get_config(self, interval, lacp_mode):
            pass
    

    另一种选择是使用pytest-ordering 插件并动态添加订单

    def data_provider():
        i = 0
        for data in [("fast", "active"), ("fast", "passive"), ("slow", "active"), ("slow", "passive")]:
            i += 1
            yield pytest.param(data[0], data[1], marks=pytest.mark.run(order=i))
    
    
    class TestExample:
    
        @pytest.mark.parametrize('interval, lacp_mode', data_provider())
        def test_edit_config(self, interval, lacp_mode):
            pass
    
        @pytest.mark.parametrize('interval, lacp_mode', data_provider())
        def test_get_config(self, interval, lacp_mode):
            pass
    

    【讨论】:

      【解决方案2】:

      测试彼此独立运行。

      如果您希望某些操作/测试一起运行,它们应该是一个测试的一部分(如果您需要设置和拆除操作,答案是不同的)

      您可以将其组织在子函数中,然后由您的测试调用(正如我在回答的第二部分工作时看到 Guy 建议的那样):

      @pytest.mark.parametrize("interval, lacp_mode", [("fast", "active"), ("fast", "passive"), ("slow", "active"),
                                                       ("slow", "passive")])
      class Example:
          def test_config(self, interval, mode):
              self.do_get_config_test(interval, mode)
              self.do_edit_config_test(interval, mode)
      
          def do_get_config_test(self, interval, mode):
              pass
          def do_edit_config_test(self, interval, mode):
              pass
      

      但我首先要问,你为什么要这样做?将它们作为单独的测试会更有意义。也许您真正想要的是单独的测试,但是使用代码运行 before 'edit' 测试来设置所需的内容(由 get_config 测试设置的内容)。我发现最好的方法是使用固定装置。

      你可以有类似的东西:

      import pytest
      
      @pytest.fixture(params=['fast', 'slow'])
      def interval(request):
          return request.param
      
      @pytest.fixture(params=['active', 'passive'])
      def mode(request):
          return request.param
      
      @pytest.fixture
      def config(interval, mode):
          # set up and return the config for the edit test
          pass
      
      def test_edit_config(config):
          pass
      def test_get_config(interval, mode):
          pass
      

      然后,'config' 夹具可以为编辑配置测试进行任何必要的设置,使其独立于获取配置测试。那么顺序就无关紧要了。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-02-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-08
        • 2020-11-27
        相关资源
        最近更新 更多