pytest封神之路第三步 精通fixture

在《pytest封神之路第三步 精通fixture》和《pytest封神之路第四步 内置和自定义marker》两篇文章中,都提到了pytest参数化。那么本文就趁着热乎,赶紧聊一聊pytest的参数化是怎么玩的。

@pytest.mark.parametrize

@pytest.mark.parametrize("test_input,expected", [("3+5", 8), ("2+4", 6), ("6*9", 42)])deftest_eval(test_input, expected):asserteval(test_input) == expected

可以自定义变量,test_input对应的值是"3+5" "2+4" "6*9",expected对应的值是8 6 42,多个变量用tuple,多个tuple用list

参数化的变量是引用而非复制,意味着如果值是list或dict,改变值会影响后续的test

重叠产生笛卡尔积

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

@pytest.fixture()

@pytest.fixture(scope="module", params=["smtp.gmail.com", "mail.python.org"])defsmtp_connection(request):smtp_connection = smtplib.SMTP(request.param,587, timeout=5)

只能使用request.param来引用

参数化生成的test带有ID,可以使用-k来筛选执行。默认是根据函数名[参数名]来的,可以使用ids来定义

// list@pytest.fixture(params=[0, 1], ids=["spam", "ham"])// function@pytest.fixture(params=[0, 1], ids=idfn)

使用--collect-only命令行参数可以看到生成的IDs。

参数添加marker

我们知道了参数化后会生成多个tests,如果有些test需要marker,可以用pytest.param来添加

marker方式

# content of test_expectation.pyimportpytest@pytest.mark.parametrize("test_input,expected",    [("3+5",8), ("2+4",6), pytest.param("6*9",42, marks=pytest.mark.xfail)],)deftest_eval(test_input, expected):asserteval(test_input) == expected

fixture方式

# content of test_fixture_marks.pyimportpytest@pytest.fixture(params=[0, 1, pytest.param(2, marks=pytest.mark.skip)])defdata_set(request):returnrequest.paramdeftest_data(data_set):pass

pytest_generate_tests

用来自定义参数化方案。使用到了hook,hook的知识我会写在《pytest hook》中,欢迎关注公众号dongfanger获取最新文章。

# content of conf.pydefpytest_generate_tests(metafunc):if"test_input"inmetafunc.fixturenames:        metafunc.parametrize("test_input", [0,1])

# content of test.pydeftest(test_input):asserttest_input ==0

定义在conftest.py文件中

metafunc有5个属性,fixturenames,module,config,function,cls

metafunc.parametrize() 用来实现参数化

多个metafunc.parametrize() 的参数名不能重复,否则会报错

参数化误区

在讲示例之前,先简单分享我的菜鸡行为。假设我们现在需要对50个接口测试,验证某一角色的用户访问这些接口会返回403。我的做法是,把接口请求全部参数化了,test函数里面只有断言,伪代码大致如下

defapi():params = []deffunc():returnrequest()    params.append(func)    ...@pytest.mark.parametrize('req', api())deftest():res = req()assertres.status_code ==403

这样参数化以后,会产生50个tests,如果断言失败了,会单独标记为failed,不影响其他test结果。咋一看还行,但是有个问题,在回归的时候,可能只需要验证其中部分接口,就没有办法灵活的调整,必须全部跑一遍才行。这是一个相对错误的示范,至于正确的应该怎么写,相信每个人心中都有一个答案,能解决问题就是ok的。我想表达的是,参数化要适当,不要滥用,最好只对测试数据做参数化

实践

本文的重点来了,参数化的语法比较简单,实际应用是关键。这部分通过11个例子,来实践一下。示例覆盖的知识点有点多,建议留大段时间细看。

1.使用hook添加命令行参数--all,"param1"是参数名,带--all参数时是range(5) == [0, 1, 2, 3, 4],生成5个tests。不带参数时是range(2)。

# content of test_compute.pydeftest_compute(param1):assertparam1 <4

# content of conftest.pydefpytest_addoption(parser):parser.addoption("--all", action="store_true", help="run all combinations")defpytest_generate_tests(metafunc):if"param1"inmetafunc.fixturenames:ifmetafunc.config.getoption("all"):            end =5else:            end =2metafunc.parametrize("param1", range(end))

2.testdata是测试数据,包括2组。test_timedistance_v0不带ids。test_timedistance_v1带list格式的ids。test_timedistance_v2的ids为函数。test_timedistance_v3使用pytest.param同时定义测试数据和id。

# content of test_time.pyfromdatetimeimportdatetime, timedeltaimportpytesttestdata = [    (datetime(2001,12,12), datetime(2001,12,11), timedelta(1)),    (datetime(2001,12,11), datetime(2001,12,12), timedelta(-1)),]@pytest.mark.parametrize("a,b,expected", testdata)deftest_timedistance_v0(a, b, expected):diff = a - bassertdiff == expected@pytest.mark.parametrize("a,b,expected", testdata, ids=["forward", "backward"])deftest_timedistance_v1(a, b, expected):diff = a - bassertdiff == expecteddefidfn(val):ifisinstance(val, (datetime,)):# note this wouldn't show any hours/minutes/secondsreturnval.strftime("%Y%m%d")@pytest.mark.parametrize("a,b,expected", testdata, ids=idfn)deftest_timedistance_v2(a, b, expected):diff = a - bassertdiff == expected@pytest.mark.parametrize("a,b,expected",    [        pytest.param(            datetime(2001,12,12), datetime(2001,12,11), timedelta(1), id="forward"),        pytest.param(            datetime(2001,12,11), datetime(2001,12,12), timedelta(-1), id="backward"),    ],)deftest_timedistance_v3(a, b, expected):diff = a - bassertdiff == expected

3.兼容unittest的testscenarios

# content of test_scenarios.pydefpytest_generate_tests(metafunc):idlist = []    argvalues = []forscenarioinmetafunc.cls.scenarios:        idlist.append(scenario[0])        items = scenario[1].items()        argnames = [x[0]forxinitems]        argvalues.append([x[1]forxinitems])    metafunc.parametrize(argnames, argvalues, ids=idlist, scope="class")scenario1 = ("basic", {"attribute":"value"})scenario2 = ("advanced", {"attribute":"value2"})classTestSampleWithScenarios:scenarios = [scenario1, scenario2]deftest_demo1(self, attribute):assertisinstance(attribute, str)deftest_demo2(self, attribute):assertisinstance(attribute, str)

4.初始化数据库连接

# content of test_backends.pyimportpytestdeftest_db_initialized(db):# a dummy testifdb.__class__.__name__ =="DB2":        pytest.fail("deliberately failing for demo purposes")

# content of conftest.pyimportpytestdefpytest_generate_tests(metafunc):if"db"inmetafunc.fixturenames:        metafunc.parametrize("db", ["d1","d2"], indirect=True)classDB1:"one database object"classDB2:"alternative database object"@pytest.fixturedefdb(request):ifrequest.param =="d1":returnDB1()elifrequest.param =="d2":returnDB2()else:raiseValueError("invalid internal test config")

5.如果不加indirect=True,会生成2个test,fixt的值分别是"a"和"b"。如果加了indirect=True,会先执行fixture,fixt的值分别是"aaa"和"bbb"。indirect=True结合fixture可以在生成test前,对参数变量额外处理。

importpytest@pytest.fixturedeffixt(request):returnrequest.param *3@pytest.mark.parametrize("fixt", ["a", "b"], indirect=True)deftest_indirect(fixt):assertlen(fixt) ==3

6.多个参数时,indirect赋值list可以指定某些变量应用fixture,没有指定的保持原值。

# content of test_indirect_list.pyimportpytest@pytest.fixture(scope="function")defx(request):returnrequest.param *3@pytest.fixture(scope="function")defy(request):returnrequest.param *2@pytest.mark.parametrize("x, y", [("a", "b")], indirect=["x"])deftest_indirect(x, y):assertx =="aaa"asserty =="b"

7.兼容unittest参数化

# content of ./test_parametrize.pyimportpytestdefpytest_generate_tests(metafunc):# called once per each test functionfuncarglist = metafunc.cls.params[metafunc.function.__name__]    argnames = sorted(funcarglist[0])    metafunc.parametrize(        argnames, [[funcargs[name]fornameinargnames]forfuncargsinfuncarglist]    )classTestClass:# a map specifying multiple argument sets for a test methodparams = {"test_equals": [dict(a=1, b=2), dict(a=3, b=3)],"test_zerodivision": [dict(a=1, b=0)],    }deftest_equals(self, a, b):asserta == bdeftest_zerodivision(self, a, b):withpytest.raises(ZeroDivisionError):            a / b

8.在不同python解释器之间测试对象序列化。python1把对象pickle-dump到文件。python2从文件中pickle-load对象。

"""

module containing a parametrized tests testing cross-python

serialization via the pickle module.

"""importshutilimportsubprocessimporttextwrapimportpytestpythonlist = ["python3.5","python3.6","python3.7"]@pytest.fixture(params=pythonlist)defpython1(request, tmpdir):picklefile = tmpdir.join("data.pickle")returnPython(request.param, picklefile)@pytest.fixture(params=pythonlist)defpython2(request, python1):returnPython(request.param, python1.picklefile)classPython:def__init__(self, version, picklefile):self.pythonpath = shutil.which(version)ifnotself.pythonpath:            pytest.skip("{!r} not found".format(version))        self.picklefile = picklefiledefdumps(self, obj):dumpfile = self.picklefile.dirpath("dump.py")        dumpfile.write(            textwrap.dedent(r"""

                import pickle

                f = open({!r}, 'wb')

                s = pickle.dump({!r}, f, protocol=2)

                f.close()

                """.format(                    str(self.picklefile), obj                )            )        )        subprocess.check_call((self.pythonpath, str(dumpfile)))defload_and_is_true(self, expression):loadfile = self.picklefile.dirpath("load.py")        loadfile.write(            textwrap.dedent(r"""

                import pickle

                f = open({!r}, 'rb')

                obj = pickle.load(f)

                f.close()

                res = eval({!r})

                if not res:

                raise SystemExit(1)

                """.format(                    str(self.picklefile), expression                )            )        )        print(loadfile)        subprocess.check_call((self.pythonpath, str(loadfile)))@pytest.mark.parametrize("obj", [42, {}, {1: 3}])deftest_basic_objects(python1, python2, obj):python1.dumps(obj)    python2.load_and_is_true("obj == {}".format(obj))

9.假设有个API,basemod是原始版本,optmod是优化版本,验证二者结果一致。

# content of conftest.pyimportpytest@pytest.fixture(scope="session")defbasemod(request):returnpytest.importorskip("base")@pytest.fixture(scope="session", params=["opt1", "opt2"])defoptmod(request):returnpytest.importorskip(request.param)

# content of base.pydeffunc1():return1

# content of opt1.pydeffunc1():return1.0001

# content of test_module.pydeftest_func1(basemod, optmod):assertround(basemod.func1(),3) == round(optmod.func1(),3)

10.使用pytest.param添加marker和id。

# content of test_pytest_param_example.pyimportpytest@pytest.mark.parametrize("test_input,expected",    [        ("3+5",8),        pytest.param("1+7",8, marks=pytest.mark.basic),        pytest.param("2+4",6, marks=pytest.mark.basic, id="basic_2+4"),        pytest.param("6*9",42, marks=[pytest.mark.basic, pytest.mark.xfail], id="basic_6*9"),    ],)deftest_eval(test_input, expected):asserteval(test_input) == expected

11.使用pytest.raises让部分test抛出Error。

fromcontextlibimportcontextmanagerimportpytest//3.7+fromcontextlibimportnullcontextasdoes_not_raise@contextmanagerdefdoes_not_raise():yield@pytest.mark.parametrize("example_input,expectation",    [        (3, does_not_raise()),        (2, does_not_raise()),        (1, does_not_raise()),        (0, pytest.raises(ZeroDivisionError)),    ],)deftest_division(example_input, expectation):"""Test how much I know division."""withexpectation:assert(6/ example_input)isnotNone

深圳网站建设www.sz886.com

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,589评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,615评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,933评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,976评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,999评论 6 393
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,775评论 1 307
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,474评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,359评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,854评论 1 317
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,007评论 3 338
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,146评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,826评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,484评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,029评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,153评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,420评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,107评论 2 356