pytest

How to run unittest discover from “python setup.py test”?

雨燕双飞 提交于 2019-11-27 05:13:15
问题 I'm trying to figure out how to get python setup.py test to run the equivalent of python -m unittest discover . I don't want to use a run_tests.py script and I don't want to use any external test tools (like nose or py.test ). It's OK if the solution only works on python 2.7. In setup.py , I think I need to add something to the test_suite and/or test_loader fields in config, but I can't seem to find a combination that works correctly: config = { 'name': name, 'version': version, 'url': url,

关于面试总结12-接口自动化面试题

删除回忆录丶 提交于 2019-11-27 04:57:49
转载: https://www.cnblogs.com/yoyoketang/p/10220061.html 关于面试总结12-接口自动化面试题 前言 前面总结了一篇关于接口测试的常规面试题,现在接口自动化测试用的比较多,也是被很多公司看好。那么想做接口自动化测试需要具备哪些能力呢? 也就是面试的过程中,面试官会考哪些问题,知道你是不是真的做过接口自动化测试?总的来说问的比较多的就是以下几个问题: 1.json和字典的区别? -对基础数据类型的考察 2.测试的数据你放在哪? -数据与脚本分离 3.参数化 - 数据驱动模式 4.下个接口请求参数依赖上个接口的返回数据 - 参数关联 5.依赖于登录的接口如何处理 -token和session的管理 6.依赖第三方的接口如何处理 -mock模拟数据返回 7.不可逆的操作,如何处理,比如删除一个订单这种接口如何测试 -造数据 8.接口产生的垃圾数据如何清理 - 数据清理 9.一个订单的几种状态如何全部测到,如:未处理,处理中,处理失败,处理成功 - 造数据,改数据库订单状态 10.python如何连接数据库操作? 11.其它的就是运行出报告、代码管理(git)、运行策略和持续集成jenkins相关了 1.json和字典dict的区别? 现在自动化培训烂大街,是个人都能说的上几个框架,面试如果问框架相关问题,求职者只需一瓶82年的雪碧

How to skip a pytest using an external fixture?

北城以北 提交于 2019-11-27 03:11:28
问题 Background I am running a py.test with a fixture in a conftest file. You can see the code below(this all works fine): example_test.py import pytest @pytest.fixture def platform(): return "ios" @pytest.mark.skipif("platform == 'ios'") def test_ios(platform): if platform != 'ios': raise Exception('not ios') def test_android_external(platform_external): if platform_external != 'android': raise Exception('not android') conftest.py import pytest @pytest.fixture def platform_external(): return

How to monkeypatch python's datetime.datetime.now with py.test?

坚强是说给别人听的谎言 提交于 2019-11-27 02:34:16
问题 I need to test functions which uses datetime.datetime.now() . What is the easiest way to do this? 回答1: You need to monkeypatch datetime.now function. In example below, I'm creating fixture which I can re-use later in other tests: import datetime import pytest FAKE_TIME = datetime.datetime(2020, 12, 25, 17, 5, 55) @pytest.fixture def patch_datetime_now(monkeypatch): class mydatetime: @classmethod def now(cls): return FAKE_TIME monkeypatch.setattr(datetime, 'datetime', mydatetime) def test

How to speed up py.test

寵の児 提交于 2019-11-27 02:02:49
问题 Is there some way to speed up the repeated execution of py.test ? It seems to spend a lot of time collecting tests, even if I specify which files to execute on the command line. I know it isn't a disk speed issue either since running pyflakes across all the .py files is very fast. 回答1: Using the norecursedirs option in pytest.ini or tox.ini can save a lot of collection time, depending on what other files you have in your working directory. My collection time is roughly halved for a suite of

How to test a function with input call?

我怕爱的太早我们不能终老 提交于 2019-11-27 01:28:21
问题 I have a console program written in Python. It asks the user questions using the command: some_input = input('Answer the question:', ...) How would I test a function containing a call to input using pytest? I wouldn't want to force a tester to input text many many times only to finish one test run. 回答1: You should probably mock the built-in input function, you can use the teardown functionality provided by pytest to revert back to the original input function after each test. import module #

KeyError in module 'threading' after a successful py.test run

僤鯓⒐⒋嵵緔 提交于 2019-11-26 23:47:36
问题 I'm running a set of tests with py.test. They pass. Yippie! But I'm getting this message: Exception KeyError: KeyError(4427427920,) in <module 'threading' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.pyc'> ignored How should I go about tracking down the source of that? (I'm not using threading directly, but am using gevent.) 回答1: I observed a similar issue and decided to see what's going on exactly - let me describe my findings. I hope someone will

How to skip the rest of tests in the class if one has failed?

99封情书 提交于 2019-11-26 22:08:34
I'm creating the test cases for web-tests using Jenkins, Python, Selenium2(webdriver) and Py.test frameworks. So far I'm organizing my tests in the following structure: each Class is the Test Case and each test_ method is a Test Step . This setup works GREAT when everything is working fine, however when one step crashes the rest of the "Test Steps" go crazy. I'm able to contain the failure inside the Class (Test Case) with the help of teardown_class() , however I'm looking into how to improve this. What I need is somehow skip(or xfail) the rest of the test_ methods within one class if one of

How to parametrize a Pytest fixture

筅森魡賤 提交于 2019-11-26 20:19:55
问题 Consider the following Pytest: import pytest class TimeLine(object): instances = [0, 1, 2] @pytest.fixture def timeline(): return TimeLine() def test_timeline(timeline): for instance in timeline.instances: assert instance % 2 == 0 if __name__ == "__main__": pytest.main([__file__]) The test test_timeline uses a Pytest fixture, timeline , which itself has the attribute instances . This attribute is iterated over in the test, so that the test only passes if the assertion holds for every instance

Logging within py.test tests

落爺英雄遲暮 提交于 2019-11-26 18:59:14
问题 I would like to put some logging statements within test function to examine some state variables. I have the following code snippet: import pytest,os import logging logging.basicConfig(level=logging.DEBUG) mylogger = logging.getLogger() ############################################################################# def setup_module(module): ''' Setup for the entire module ''' mylogger.info('Inside Setup') # Do the actual setup stuff here pass def setup_function(func): ''' Setup for test