pytest

pytest框架: fixture之conftest.py

随声附和 提交于 2019-12-02 19:26:01
原文地址: https://blog.csdn.net/BearStarX/article/details/101000516 一、fixture优势 1、fixture相对于setup和teardown来说应该有以下几点优势: 命名方式灵活,不局限于setup和teardown这几个命名 conftest.py配置 里可以实现数据共享,不需要import就能自动找到一些配置 scope="module"可以实现多个.py跨文件共享前置 scope="session"以实现多个.py跨文件使用一个session来完成多个用例 2、使用装饰器标记fixture的功能 fixture(scope="function",params=None,autouse=False,ids=None,name=None) 可以使用此装饰器(带或不带参数)来定义fixture功能。fixture功能的名称可以在以后使用,引用它会在运行测试之前调用它: test模块或类可以使用pytest.mark.usefixtures(fixturename)标记 测试功能可以直接使用fixture名称作为输入参数,在这种情况下,夹具实例从fixture返回功能将被注入 二、fixture参数介绍 fixture(scope="function",params=None,autouse=False,ids=None

Python最佳工程实践,建立一个完美的工程项目

旧巷老猫 提交于 2019-12-02 18:07:47
在程序开发时候一套好的开发环境和工具栈,可以帮我们极大的提高开发的效率,避免把大量时间浪费在周边琐事上。本文以Python为例,教大家如何快速打造完美的Python项目开发环境:内容涵盖了模块依赖管理、代码风格管理、调试测试管理和Git版本管理,使用git hook做项目规范检查等。 pipx Pipx是一款跨平台的Python环境隔离管理工具,可以在支持在 Linux、Mac OS 和 Windows 上运行。Pipx默认在是个人用户下建立虚拟Python环境,并以此建立实现完全隔离的Python运行环境。安装pipx需要Pthon 3.6及以上版本: python3 -m pip install --user pipxpython3 -m pipx ensurepath 升级Pipx使用: python3 -m pip install -U pipx 包依赖管理pipenv Pipenv会自动为你的项目创建和管理虚拟环境,以pipfile文件方式方式管理项目的依赖包,支持包的安装和卸载。和requirements.txt不同,pipfile是TOML格式,支持开发环境与正式环境,还可以使用Pipfile.lock锁定环境版本。pipxenv的安装可以使用pipx: pipx install pipenv 有些发行版也是可以直接通过其包管理器安装的: 比如MacOS可以下可以使用

pytest cannot import module while python can

我怕爱的太早我们不能终老 提交于 2019-12-02 17:13:52
I am working on a package in Python. I use virtualenv. I set the path to the root of the module in a .pth path in my virtualenv, so that I can import modules of the package while developing the code and do testing (Question 1: is it a good way to do?). This works fine (here is an example, this is the behavior I want): (VEnvTestRc) zz@zz:~/Desktop/GitFolders/rc$ python Python 2.7.12 (default, Jul 1 2016, 15:12:24) [GCC 5.4.0 20160609] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> from rc import ns >>> exit() (VEnvTestRc) zz@zz:~/Desktop/GitFolders/rc$

Python - Use pytest to test test methods written in subclass

白昼怎懂夜的黑 提交于 2019-12-02 16:58:41
问题 I am a novice to pytest. I have a scenario wherein i wanted to test some test methods written in a subclass. Assume that the following is my code structure class Superclass: def __init__(self, a): self.a = a def dummy_print(): print("This is a dummy function") class TestSubClass(Superclass): def test_1_eq_1(): assert 1 == 1 Upon executing the following command py.test -s -v test_filename.py I get the following error messgae: cannot collect test class 'Test_Super' because it has a init

pytest8-skip与xfail

我与影子孤独终老i 提交于 2019-12-02 16:15:39
skip(无条件跳过测试用例)与skipif(有条件跳过测试用例) # test_skip_function.py 函数级别 import pytest import sys @pytest.mark.skip(reason='no way of currently testing this') def test_the_unknown(): assert 1 == 1 @pytest.mark.skipif(sys.version_info < (3, 7), reason="requires python3.7 or higher") # 有条件跳过测试用例 def test_function(): assert 1 == 1 输出结果:D:\myproject\pytest_demo>pytest -qs --tb=no test_skip_function.py ss 2 skipped in 0.02 seconds # test_skip_class.py 类级别 import pytest import sys @pytest.mark.skipif(sys.version_info < (3, 7), reason="requires python3.6 or higher") class TestSkipClass: def test_class(self):

unittest Vs pytest

半腔热情 提交于 2019-12-02 14:39:28
In unittest, I can setUp variables in a class and then the methods of this class can chose whichever variable it wants to use... class test_class(unittest.TestCase): def setUp(self): self.varA = 1 self.varB = 2 self.varC = 3 self.modified_varA = 2 def test_1(self): do_something_with_self.varA, self.varB def test_2(self): do_something_with_self_modified_varA, self.varC So in unittest, it was easy to put bunch of tests together that could go under one class and then use many different variables (varA and varB) for different methods. In pytest, I created a fixture in conftest.py instead of a

Pytest权威教程-更改标准(Python)测试发现

不羁的心 提交于 2019-12-02 12:01:19
目录 更改标准(Python)测试发现 在测试收集过程中忽略路径 测试期间收集的测试取消 保留从命令行指定的重复路径 更改目录递归 更改命名约定 将cmdline参数解释为Python包 找出收集的东西 自定义测试集 更改标准(Python)测试发现 在测试收集过程中忽略路径 通过--ignore=path在cli上传递选项,可以轻松地在收集过程中忽略某些测试目录和模块。pytest允许多个 --ignore选项。例: tests/ |-- example | |-- test_example_01.py | |-- test_example_02.py | '-- test_example_03.py |-- foobar | |-- test_foobar_01.py | |-- test_foobar_02.py | '-- test_foobar_03.py '-- hello '-- world |-- test_world_01.py |-- test_world_02.py '-- test_world_03.py 现在,如果你调用pytest使用,你会发现只收集测试模块,这不符合指定的模式:--ignore=tests/foobar/test_foobar_03.py --ignore=tests/hello/pytest =====================

Running pytest with cython - how to compile cython modules in pytest?

风流意气都作罢 提交于 2019-12-02 10:27:21
问题 I have a project organized as follows: project ├── project │ ├── module1 │ │ ├── api.py │ │ ├── _cpython_foo.py │ │ └── _cython_foo.pyx │ └── module2 ├── setup.py └── tests └── module1 ├── test_cython_foo.py └── test_cpython_foo.py where api.py imports cythonized extensions: """api.py"""" from _cython_foo import cython_fun My setup script builds the .pyx source inplace correctly and I'm able to use cython_fun in the installed package: import project.module1.api as module1 module1.cython_fun()

Running unit tests in parallel with pytest?

你。 提交于 2019-12-02 10:05:41
How can I parallelize the execution of the unit tests written with pytest? Which tactics of parallelism can I choose from? Andriy Ivaneyko In order to run pytests in parallel you are going to need to install pytest-xdist . Please see the different parallelism tactics listed below, you can use any of those (however I can bet that one of those suits best for your particular case): pip install pytest-xdist # The most primitive case, sending tests to multiple CPUs: pytest -n NUM # Execute tests within 3 subprocesses. pytest --dist=each --tx 3*popen//python=python3.6 # Execute tests in 3 forked

Python - Use pytest to test test methods written in subclass

丶灬走出姿态 提交于 2019-12-02 09:57:25
I am a novice to pytest. I have a scenario wherein i wanted to test some test methods written in a subclass. Assume that the following is my code structure class Superclass: def __init__(self, a): self.a = a def dummy_print(): print("This is a dummy function") class TestSubClass(Superclass): def test_1_eq_1(): assert 1 == 1 Upon executing the following command py.test -s -v test_filename.py I get the following error messgae: cannot collect test class 'Test_Super' because it has a init constructor The same is mentioned in the pytest documentation as well. Is there a workaround for this? I need