pytest parametrize参数化标记

介绍

parametrize装饰器可以对函数进行参数化,像用例相同场景不同数据的时候就可以使用它

函数参数化


import pytest
@pytest.mark.parametrize("nub1,nub2,nub3",[[1,2,3],[2,2,4],[3,2,5]])
def test_01(nub1,nub2,nub3):
    assert nub1+nub2 == nub3

类参数化

import pytest


@pytest.mark.parametrize("nub1,nub2,nub3",[[1,2,3],[2,2,4],[3,2,5]])
class Test001():

    def test_01(self,nub1,nub2,nub3):
        assert nub1+nub2 == nub3

    def test_02(self,nub1,nub2,nub3):
        assert nub1+nub2 == nub3

模块参数化

import pytest


pytestmark = pytest.mark.parametrize("nub1,nub2,nub3",[[1,2,3],[2,2,4],[3,2,5]])
class Test001():

  def test_01(self,nub1,nub2,nub3):
      assert nub1+nub2 == nub3

  def test_02(self,nub1,nub2,nub3):
      assert nub1+nub2 == nub3


class Test002():

  def test_01(self,nub1,nub2,nub3):
      assert nub1+nub2 == nub3

  def test_02(self,nub1,nub2,nub3):
      assert nub1+nub2 == nub3

需要多个参数化组合,可以使用多个parametrize装饰器

import pytest

@pytest.mark.parametrize("x", [0, 1])
@pytest.mark.parametrize("y", [2, 3])
def test_foo(x, y):
    pass

这将按照装饰器的顺序将参数设置为x=0/y=2、x=1/y=2、 x=0/y=3和耗尽参数运行测试。x=1/y=3

posted @ 2022-06-21 09:54  zhq9  阅读(40)  评论(0编辑  收藏  举报