pytest

Why is caplog.text empty, even though the function I'm testing is logging?

我只是一个虾纸丫 提交于 2020-01-25 06:43:06
问题 I'm trying to use pytest to test if my function is logging the expected text, such as addressed this question (the pyunit equivalent would be assertLogs). Following the pytest logging documentation, I am passing the caplog fixture to the tester. The documentation states: Lastly all the logs sent to the logger during the test run are made available on the fixture in the form of both the logging.LogRecord instances and the final log text. The module I'm testing is: import logging logger =

temp files/directories with custom names?

限于喜欢 提交于 2020-01-24 19:53:27
问题 How to create a temporary files/directories with user defined names in python. I am aware of tempfile . However I couldn't see any function with filename as argument. Note: I need this for unit testing the glob(file name pattern matching) functionality on a temporary directory containing temporary files rather than using the actual file system. 回答1: You can use open() with whatever file name you need. e.g. open(name, 'w') Open Or import os import tempfile print 'Building a file name yourself:

Python nested subpackage doesn't import when running tests

巧了我就是萌 提交于 2020-01-24 09:29:27
问题 I have a folder structure like this: api -- test -- test_api.py -- __init__.py -- api -- api.py -- __init__.py -- sub -- sub.py -- __init__.py sub.py : Base = 'base' api.py : from sub.sub import Base def stuff_to_test(): pass test_api.py : from api.api import stuff_to_test def test_stuff_to_test(): stuff_to_test() I am in directory api . I run pytest : ==================================== ERRORS ==================================== ______________________ ERROR collecting tests/test_api.py ___

Mocking a imported function with pytest [duplicate]

二次信任 提交于 2020-01-23 12:42:13
问题 This question already has answers here : Python Mocking a function from an imported module (2 answers) Closed 2 years ago . I would like to test a email sending method I wrote. In file, format_email.py I import send_email. from cars.lib.email import send_email class CarEmails(object): def __init__(self, email_client, config): self.email_client = email_client self.config = config def send_cars_email(self, recipients, input_payload): After formatting the email content in send_cars_email() I

Mocking a imported function with pytest [duplicate]

放肆的年华 提交于 2020-01-23 12:41:33
问题 This question already has answers here : Python Mocking a function from an imported module (2 answers) Closed 2 years ago . I would like to test a email sending method I wrote. In file, format_email.py I import send_email. from cars.lib.email import send_email class CarEmails(object): def __init__(self, email_client, config): self.email_client = email_client self.config = config def send_cars_email(self, recipients, input_payload): After formatting the email content in send_cars_email() I

Django_01_创建图书管理项目

落爺英雄遲暮 提交于 2020-01-23 07:19:25
在django中,项目的组织结构为一个项目包含多个应用,一个应用对应一个业务模块 示例:创建项目的名称为test1,完成“图书-英雄”信息的维护,创建应用名称为booktest 创建项目:首先进入到虚拟环境中:workon django_py2,在当前用户的某个目录下创建项目,这样不会发生权限问题,此处在/home/Desktop/pytest/目录下创建项目   cd /home/Desktop/   mkdir pytest   cd pytest 创建项目的命令如下: django-admin startproject 项目名称  例:django-admin startproject test1   接下来可以使用IDE打开此目录,开发项目了,此处使用 pycharm 打开pytest目录 项目默认目录说明 进入test1目录,查看目录树形结构   cd test1   tree 目录结构如下图 manage.py是项目运行的入口,指定配置文件路径 与项目同名的目录,此处为test1,包含项目的配置文件 init.py是一个空文件,作用是这个目录test1可以被当作包使用 settings.py是项目的整体配置文件 urls.py是项目的URL配置文件 wsgi.py是项目与WSGI兼容的Web服务器入口,详细内容会在part6的布署中讲到 创建应用

Writting py test for sqlalchemy app

假如想象 提交于 2020-01-23 01:00:08
问题 I am trying convert unit test into py test. I am using the unit test example class TestCase(unittest.TestCase): def setUp(self): app.config['TESTING'] = True app.config['CSRF_ENABLED'] = False app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'test.db') db.create_all() def tearDown(self): db.session.remove() db.drop_all() I am not sure, What should be its py test version. 回答1: I searched high and low for a well explained solution to use SqlAlchemy without Flask

Parallely running parameterized tests in pytest

自作多情 提交于 2020-01-22 20:01:07
问题 I wanted to run parameterized test functions in parallel. This is for a concurrency testing scenario. Same testcase runs in parallel with different parameters in a device. After completing all the parameterized variant of one test function, I want to proceed with the next one. If we take this simple example, I want to run all 4 instances of test_even parallely and then move to test_odd. @pytest.mark.parametrize("x", range(4)) def test_even(x): assert x % 2 == 0 @pytest.mark.parametrize("x",

Python测试框架之unittest和pytest

梦想与她 提交于 2020-01-20 21:02:16
目前搜狗商城接口测试框架用的是unittest+HTMLTestRunner,case数有1097条,目前运行一次自动化测试,时长约为30分钟,期望控制在10分钟或者更短的时间内。近期打算重新优化框架,着重解决运行效率低的问题。最近调研了一下另一种主流测试框架Pytest,Pytest是一个非常成熟的全功能的Python测试框架,本文主要对比了Unittest和Pytest这两种较为流行的Python测试框架。 一、Unittest Unittest是Python标准库中自带的单元测试框架,Unittest有时候也被称为PyUnit,就像JUnit是Java语言的标准单元测试框架一样,Unittest则是Python语言的标准单元测试框架。 Unittest支持自动化测试,测试用例的初始化、关闭和测试用例的聚合等功能,它有一个很重要的特性:它是通过类(class)的方式,将测试用例组织在一起。 示例: 执行结果: 注:unittest有一个关联模块unittest2,但unittest2仅适用于Python 2.4-2.6。这是由于从Python 2.7开始,unittest增加一些新的特性。为了在老的版本中支持这些特性,所以提供了unittest2这个库。但对于Python 2.7及之后的版本,unittest是唯一的。本次示例中使用的为python2.7。 二、Pytest

pytest配置文件:

核能气质少年 提交于 2020-01-20 20:59:28
目录结构: pytest.ini [pytest]addopts = -s -vtestpaths = ./scriptspython_files = test_*.pypython_classes = Test*python_functions = test_* test_case.py def test_case_01(): assert 1class TestCase(object): def test_case_01(self): """再执行三""" assert 1 def test_case_02(self): """再执行六""" assert {"title":"v2ex"} != {"title":"V2EX"} def test_case_03(self): """再执行九""" assert 1 def setup_class(self): """先执行一setup_class 类级别的setup""" print("类级别的setup") def teardown_class(self): """再执行十一teardown_class 类级别的teardown""" print("类级别的teardown") def setup_method(self): """再执行二setup_method 类中方法级别的setup""" """再执行五setup