pytest

python + pytest基本使用方法(参数化)

江枫思渺然 提交于 2019-12-05 03:17:05
import pytestimport math#pytest 参数化#'base,exponent,expected'用来定义参数的名称。# 通过数组定义参数时,每一个元组都是一条测试用例使用的测试数据。# ids参数默认为None,用于定义测试用例的名称# math模块的pow()方法用于计算(x的y次方)的值#运行: pytest -v test_parameterize.py# ‘-v’ 参数增加测试用例冗长@pytest.mark.parametrize( 'base,exponent,expected', [ (2,2,4), (2,3,8), (1,9,1), (0,9,0), ],ids=['case1','case2','case3','case4'])def test_pow(base,exponent,expected): assert math.pow(base,exponent) == expected 来源: https://www.cnblogs.com/Teachertao/p/11902086.html

python + pytest基本使用方法(运行测试&测试报告)

佐手、 提交于 2019-12-05 03:17:01
import pytest# 1.运行名称中包含某字符串的测试用例#名称中含add 的测试用例# 执行: pytest -k add test_assert.py# 2.减少测试的运行冗长# 执行: pytest -q test_assert.py# 3.如果出现一条测试用例失败,则退出测试# 执行: pytest -x test_assert.py# 4.运行测试目录 相对路径或绝对路径都行# 执行: pytest ./test_dir# 5.指定测试类或方法执行# 指定运行test_fixtures_02.py文件中的TestMultiply类下的test_multiply_5_6方法# 文件名、类名和方法名之间用::符合分隔# 执行: pytest test_fixtures_02.py::TestMultiply::test_multiply_5_6# 6.通过main()方法运行测试# if __name__ == '__main__':# if __name__ == '__main__':# pytest(['-s','./test_dir'])#生成测试报告# 1.生成JUnit XML 文件# 执行: pytest ./test_dir --junit-xml=./report/log.xml# 2.生成在线测试报告# 执行: pytest ./test_dir

How to mock/set system date in pytest?

只愿长相守 提交于 2019-12-05 03:08:54
In some of my tests I am having a problem that they fail on Travis because of time and time zone problems, so I want to mock system time for my test. How can I do this? AFAIK, you can't mock builtin methods. One approach I have often done is to change my code a bit to not use datetime directly to obtain the date, but a wrapper function somewhere: # mymodule.py def get_today(): return datetime.date.today() This makes it trivial to just mock it in your test: def test_something(): with mock.patch('mymodule.get_today', return_value=datetime.date(2014, 6, 2)): ... You can also use the freezegun

py.test session level fixtures in setup_method

谁说胖子不能爱 提交于 2019-12-05 03:00:40
Is there a way to somehow use pytest fixtures from conftest.py in a test class's setup ? I need to initialize an object when the session starts and use it in some test classes' setup. Something like this: # conftest.py: import pytest @pytest.fixture(scope="session", autouse=True) def myfixture(request): return "myfixture" # test_aaa.py class TestAAA(object): def setup(self, method, myfixture): print("setup myfixture: {}".format(myfixture)) ... I used this kind of a setup for a test class with pytest<=3.7.0 (it stopped working with pytest 3.7.1 release): # conftest.py: import pytest # autouse

pytest执行时mian函数传参

北城余情 提交于 2019-12-05 02:48:13
在代码中执行pytest可以通过main函数 加参数来指定运行规则时,参数需要放在列表或者元祖中 # pytest.main(["--html=report.html"]) # pytest.main(["--collect-only"])#展示所有测试用例 # pytest.main(["-k","11"])#使用指定表达式运行希望运行的用例 # pytest.main(["-v","-k","11"])# 增加-v查看详细信息 # pytest.main(["-v","-m","run_first"]) """ 使用-m对用例进行标记,用例需注释@pytest.mark.xxx,将xxx作为参数传入 使用-m "mark1 and mark2"可以同时选中带有这两个标记的所有测试用例。 使用-m "mark1 and not mark2"选中带哟与mark1的测试用例,而过滤掉带有mark2的测试用例 使用-m "mark1 or mark2"则选中带有mark1或者mark2的所有测试用例 """ # pytest.main(["-v","-x"])#-x 遇到错误即停止 # pytest.main(["-v","--maxfail=2","--tb=no"])#--maxfail=n 设定最多失败 n 次即停止 # pytest.main(["-s"])

Invalid syntax in more-itertools when running pytest

梦想与她 提交于 2019-12-05 01:31:13
I have the following minimal setup.py : import setuptools setuptools.setup( setup_requires=['pytest-runner'], tests_require=['mock', 'pytest'], test_suite='tests', python_requires='>=2.7', ) when running it with python setup.py test I keep getting the following error: Traceback (most recent call last): File "setup.py", line 8, in <module> python_requires='>=2.7', File "/Users/project/tmp/env/lib/python2.7/site-packages/setuptools/__init__.py", line 145, in setup return distutils.core.setup(**attrs) File "/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7

How to debug py.test in PyCharm when coverage is enabled

女生的网名这么多〃 提交于 2019-12-05 00:02:31
How do I debug py.test in PyCharm when coverage is enabled? Coverage is enabled using --cov=project --cov-report=term-missing , removing this and breakpoints are hit. Versions: pycharm 5.0.3, pytest==2.8.5, pytest-cache==1.0, pytest-cov==2.2.0, pytest-pep8==1.0.6, pytest-xdist==1.13.1, python-coveralls==2.6.0. (thanks for jon's advice on further diagnosing the issue) Inti There is now a flag in py.test to disable coverage which you can activate when running tests from PyCharm. The flag to use is --no-cov . If you want this to apply to all your test runs you can add this to the default pytest

Can I parameterize a pytest fixture with other fixtures?

别来无恙 提交于 2019-12-04 23:37:37
问题 I have a python test that uses a fixture for credentials (a tuple of userid and password) def test_something(credentials) (userid, password) = credentials print("Hello {0}, welcome to my test".format(userid)) and I have pytest fixture for credentials: @pytest.fixture() def credentials(): return ("my_userid", "my_password") It works great Now I want to extend this for multiple credentials (say staging and production) so that my test will run twice (once each for staging and production). I

pytest常用命令行

早过忘川 提交于 2019-12-04 21:32:47
pytest --collect-only 使用--collect-only选项可以展示在给定的配置下哪些测试用例会被运行 pytest -k -k可以通过表达式运行指定的测试用例 比如pytest -k “asdict or defaults”,就指定运行test_asdict()和test_defaults()。 pytest -m marker用于标记测试并分组,以便快速选中并运行。 使用-m选项可以用表达式指定多个标记名。 使用-m “mark1 and mark2” 可以同时选中带有这两个标记的所有测试用例。 使用-m "mark1 and not mark2 " 会选中带有mark1的测试用例,过滤掉带有mark2的测试用例 使用-m “mark1 or mark2” 会选中带有mark1或者mark2的所有测试用例 -x 使用-x,在执行测试用例时,遇到测试失败,就会全局停止 --maxfail=num 使用--maxfail=2,可以指定pytest失败2次后再停止 --lf(--last-failed) 当一个或多个测试失败时,希望能够定位到最后一个失败的测试用例重新运行,可以使用--lf选项 --ff(--failed-first)选项 --ff选项与--lf选项作用基本相同,不同之处在于--ff会运行完剩余的测试用例 来源: https://www

pytest (py.test) very slow startup in cygwin

空扰寡人 提交于 2019-12-04 19:31:58
In cygwin, py.test starts up very slow. It does not look like a collection issue because of two reasons: The same test starts up quickly in linux. And sometimes, if rerun the same test fast enough in cygwin, it starts up in less than 1 second. Running through the time command tells it starts up in either 0.4 seconds, or 11.7 seconds, when supplied with the --collection-only option to avoid running the actual tests. I also added a print to the hooks pytest_configure() and pytest_ignore_collect() to be sure indeed it is before the collection starts. There have been other questions, linke how to