pytest

How do I configure PyCharm to run py.test tests?

自闭症网瘾萝莉.ら 提交于 2019-11-29 19:06:43
I want to start writing unit tests for my Python code, and the py.test framework sounds like a better bet than Python's bundled unittest . So I added a "tests" directory to my project, and added test_sample.py to it. Now I want to configure PyCharm to run all the tests in my "tests" directory. PyCharm allegedly supports py.test in its test runner. You're supposed to be able to create a run/debug configuration to run your tests, and PyCharm allegedly has a "create configuration" dialog box specifically for py.test . But that's the complete extent of their documentation on the subject, and I can

Python与C/C++相互调用(python2 调c++那个试了ok)

狂风中的少年 提交于 2019-11-29 19:01:27
一、问题 Python模块和C/C++的动态库间相互调用在实际的应用中会有所涉及,在此作一总结。 二、Python调用C/C++ 1、Python调用C动态链接库 Python调用C库比较简单,不经过任何封装打包成so,再使用python的ctypes调用即可。 (1)C语言文件:pycall.c /***gcc -o libpycall.so -shared -fPIC pycall.c*/ #include <stdio.h> #include <stdlib.h> int foo(int a, int b) { printf("you input %d and %d\n", a, b); return a+b; } (2)gcc编译生成动态库libpycall.so:gcc -o libpycall.so -shared -fPIC pycall.c。使用g++编译生成C动态库的代码中的函数或者方法时,需要使用extern "C"来进行编译。 (3)Python调用动态库的文件:pycall.py import ctypes ll = ctypes.cdll.LoadLibrary lib = ll("./libpycall.so") lib.foo(1, 3) print '***finish***' (4)运行结果: 2、Python调用C++(类)动态链接库

writing a pytest function to check outputting to a file in python?

本小妞迷上赌 提交于 2019-11-29 17:06:17
问题 I asked this question about how to write a pytest to check output in stdout and got a solution. Now I need to write a test case , to check if the contents are written to the file and that the contents are written as expected eg: def writetoafile(): file = open("output.txt",w) file.write("hello\n") file.write("world\n") file.close() now a pytest function to check if it written: def test_writeToFile(): file = open("ouput.txt",'r') expected = "hello\nworld\n" assert expected==file.read() while

py.test to test flask register, AssertionError: Popped wrong request context

守給你的承諾、 提交于 2019-11-29 16:58:29
问题 I'm using flask to do register and login: from flask.ext.security.views import register, login class Register(Resource): def post(self): return register() class Login(Resource): def post(self): return login() api.add_resource(Login, '/login') api.add_resource(Register, '/register') then I use py.test to test the class: class TestAPI: def test_survey(self, app): client = app.test_client() data = {'email': 'test@test', 'password': 'password'} rv = client.post('/2014-10-17/register', data=json

How do I disable a test using py.test?

自作多情 提交于 2019-11-29 16:34:56
问题 Say I have a bunch of tests: def test_func_one(): ... def test_func_two(): ... def test_func_three(): ... Is there a decorator or something similar that I could add to the functions to prevent from py.test from running just that test? The result might look something like... @pytest.disable() def test_func_one(): ... def test_func_two(): ... def test_func_three(): ... I have searched for something like this in the py.test docs, but I think I might be missing something here. 回答1: Pytest has the

Usual pytest workflow - can't run tests in a file

依然范特西╮ 提交于 2019-11-29 16:04:18
I'm trying to setup a project (Python 3.4.4, pytest 3.3.1) to use pytest, but pytest is failing to run tests in a file or a directory. I think I'm using pytest wrong, but am not what the correct workflow would be. What I've tried Following the suggestions in "Good Integration Practices" and also due to the personal preference of having the source and the tests in separate folders, I have setup my project as follows: parent_directory/ setup.py src/ project_name/ __init__.py # empty utils.py tests/ __init__.py # empty project_name/ __init__.py # empty test_utils.py # has `import project_name

How to I display why some tests where skipped while using py.test?

元气小坏坏 提交于 2019-11-29 15:54:10
问题 I am using skipIf() from unittest for skipping tests in certain conditions. @unittest.skipIf(condition), "this is why I skipped them!") How do I tell py.test to display skipping conditions? I know that for unittest I need to enable the verbose mode ( -v ) but the same parameter added to py.test increase the verbosity by still does not display the skip reasons. 回答1: When you run py.test, you can pass -rsx to report skipped tests. From py.test --help : -r chars show extra test summary info as

`py.test` and `__init__.py` files

天大地大妈咪最大 提交于 2019-11-29 13:51:50
I thought py.test is "standalone" in a sense that it treats test_*.py files "as it is", and only imports modules specified in these files, with no respect to any surrounding files. It looks like I'm wrong. Here is my dialog with py.test : $ ls __init__.py test_pytest.py $ cat __init__.py $ cat test_pytest.py def test_pytest(): assert True $ py.test test_pytest.py ========================================================= test session starts ========================================================== platform darwin -- Python 2.7.2 -- pytest-2.1.3 collected 0 items / 1 errors ====================

How can I access the overall test result of a pytest test run during runtime?

北战南征 提交于 2019-11-29 12:59:13
Dependent on the overall test result of a pytest test run I would like to execute conditional tear down. This means the access to the overall test result must happen after all tests have been executed but before the test runner has been left. How can I achieve this? hoefling I could not find a suitable pytest hook to access the overall test result yet. You don't need one; just collect the test results yourself. This is the blueprint I usually use when in need of accessing the test results in batch: # conftest.py import pytest def pytest_sessionstart(session): session.results = dict() @pytest

py.test does not start (EBUSY: [Resource device])

拟墨画扇 提交于 2019-11-29 11:39:34
Just installed py.test with pip install -U pytest with no errors, however we I'm trying to launch py.test I get error: EBUSY: [Resource device]: listdir('C:\\Users\\Administrator\\AppData\\Local\\Application Data',) Here is the output: C:\Users\Administrator>py.test ========================== test session starts =========================== platform win32 -- Python 2.7.3 -- pytest-2.2.4 collected 0 items / 1 errors ================================= ERRORS ================================= ___________________________ ERROR collecting . ___________________________ C:\Python27\lib\site-packages\py