使用lambda函数定义pytest fixture。

pytest-lambda的Python项目详细描述


pytest lambda

使用lambda函数定义pytest fixture。

快速启动

pip install pytest-lambda
# test_the_namerator.pyfrompytest_lambdaimportlambda_fixture,static_fixturefirst=static_fixture('John')middle=static_fixture('Jacob')last=static_fixture('Jingleheimer-Schmidt')full_name=lambda_fixture(lambdafirst,middle,last:f'{first}{middle}{last}')deftest_the_namerator(full_name):assertfull_name=='John Jacob Jingleheimer-Schmidt'

备忘单

importpytestfrompytest_lambdaimport(disabled_fixture,error_fixture,lambda_fixture,not_implemented_fixture,static_fixture,)# Basic usagefixture_name=lambda_fixture(lambdaother_fixture:'expression',scope='session',autouse=True)# Request fixtures by namefixture_name=lambda_fixture('other_fixture')fixture_name=lambda_fixture('other_fixture','another_fixture','cant_believe_its_not_fixture')# Reference `self` inside a classclassTestContext:fixture_name=lambda_fixture(lambdaself:self.__class__.__name__,bind=True)# Parametrizefixture_name=lambda_fixture(params=['a','b'])fixture_name=lambda_fixture(params=['a','b'],ids=['A!','B!'])fixture_name=lambda_fixture(params=[pytest.param('a',id='A!'),pytest.param('b',id='B!')])# Use literal value (not lazily evaluated)fixture_name=static_fixture(42)fixture_name=static_fixture('just six sevens',autouse=True,scope='module')# Raise an exception if fixture is requestedfixture_name=error_fixture(lambda:ValueError('my life has no intrinsic value'))# Or maybe don't raise the exceptionfixture_name=error_fixture(lambdaother_fixture:TypeError('nope')ifother_fixtureelseNone)# Create an abstract fixture (to be overridden by the user)fixture_name=not_implemented_fixture()fixture_name=not_implemented_fixture(autouse=True,scope='session')# Disable usage of a fixture (fail early to save future head scratching)fixture_name=disabled_fixture()

还有什么可能?

当然,您可以在测试类中使用lambda fixture:

# test_staying_classy.pyfrompytest_lambdaimportlambda_fixtureclassTestClassiness:classiness=lambda_fixture(lambda:9000+1)deftest_how_classy_we_is(self,classiness):assertclassiness==9001

对其他装置进行混叠

您还可以传递另一个fixture的名称,而不是lambda:

# test_the_bourne_identity.pyfrompytest_lambdaimportlambda_fixture,static_fixtureagent=static_fixture('Bourne')who_i_am=lambda_fixture('agent')deftest_my_identity(who_i_am):assertwho_i_am=='Bourne'

甚至可以使用多个设备名称:

# test_the_bourne_identity.pyfrompytest_lambdaimportlambda_fixture,static_fixtureagent_first=static_fixture('Jason')agent_last=static_fixture('Bourne')who_i_am=lambda_fixture('agent_first','agent_last')deftest_my_identity(who_i_am):assertwho_i_am==('Jason','Bourne')

为别名装置添加注释

您可以强制加载夹具,而不需要记住pytest.mark.usefixtures

# test_garage.pyfrompytest_lambdaimportlambda_fixture,static_fixturecar=static_fixture({'type':'Sweet-ass Cadillac','is_started':False,})turn_the_key=lambda_fixture(lambdacar:car.update(is_started=True))preconditions=lambda_fixture('turn_the_key',autouse=True)deftest_my_caddy(car):assertcar['is_started']

声明抽象事物

^ }是测试混合测试用例抽象参数固定的理想方法

# test_mixinalot.pyimportpytestfrompytest_lambdaimportstatic_fixture,not_implemented_fixtureclassDials1900MixinALot:butt_shape=not_implemented_fixture()desires=not_implemented_fixture()defit_kicks_them_nasty_thoughts(self,butt_shape,desires):assertbutt_shape=='round'and'triple X throw down'indesires@pytest.mark.xfailclassDescribeMissThing(Dials1900MixinALot):butt_shape=static_fixture('flat')desires=static_fixture(['playin workout tapes by Fonda'])classDescribeSistaICantResista(Dials1900MixinALot):butt_shape=static_fixture('round')desires=static_fixture(['gettin in yo Benz','triple X throw down'])

使用disabled_fixture将设备标记为禁用。想想看。

# test_ada.pyimportpytestfrompytest_lambdaimportdisabled_fixturewheelchair=disabled_fixture()@pytest.mark.xfail(strict=True)deftest_stairs(wheelchair):assertwheelchair+'floats'

引发异常

当请求一个fixture时,也可以使用error_fixture

# test_bikeshed.pyimportpytestfrompytest_lambdaimporterror_fixture,not_implemented_fixture,static_fixturebicycle=static_fixture('a sledgehammer')defit_does_sweet_jumps(bicycle):assertbicycle+'jump'>='3 feet'classContextOcean:depth=not_implemented_fixture()bicycle=error_fixture(lambdabicycle,depth:(RuntimeError(f'Now is not the time to use that! ({bicycle})')ifdepth>'1 league'elseNone))classContextDeep:depth=static_fixture('20,000 leagues')@pytest.mark.xfail(strict=True,raises=RuntimeError)defit_doesnt_flip_and_shit(self,bicycle):assertbicycle+'floats'classContextBeach:depth=static_fixture('1 inch')defit_gets_you_all_wet_but_otherwise_rides_like_a_champ(self,bicycle):assert'im wet'

开发

如何在本地构建和测试该对象?

  1. 创建一个virtualenv,不管你喜欢什么。或者,如果你愿意的话,不要。
  2. pip install poetry
  3. poetry install要安装setuptools入口点,pytest将自动加载插件(否则,您将不得不运行py.test -p pytest_lambda.plugin
  4. 运行py.test。测试将从readme.md中收集(感谢pytest-markdown)。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
如何使用java向dropup html/css添加项目   如何从java中的向量向量打印   Java Maven库项目模板   java使用atmosphere api还是直接使用grizzly?   java JComponent仅部分显示   如何将动态值传递给自定义注释,以从Java数据进行映射。性质   java破解已实现方法的返回类型的最佳方法?   java Netbeans在JFrame Gui布局中覆盖图像   spring java仅向登录用户显示注销按钮   java如何对com进行身份验证。谷歌。云bigquery。带有服务帐户的BigQuery`   java禁止空字符串参数和抛出RuntimeException以阻止方法继续的利弊   java分析项目中的所有JAR以获取版本和许可证信息   Java,数据库为什么要分配一个新对象,而我们可以直接将它放入数据库