草庐IT

python - 我不能忽略 Django 上的 pycache 和 db.sqlite,即使它在 .gitignore 中引用它们

我想忽略Django项目的pycache和db.sqlite的变化。我在.gitignore中引用它们,但是git捕获了它们的变化。如果你知道,你能告诉我什么是问题吗?我在句子末尾附上了我的.gitignore。.gitignore#Byte-compiled/optimized/DLLfiles__pycache__/*.py[cod]*$py.classmedia/settings.py.idea/#Cextensions*.so#Distribution/packaging.Pythonenv/build/develop-eggs/dist/downloads/eggs/.egg

python :unit test throws <Response streamed [200 OK]> instead of actual output

fromflaskimportjsonify@app.route('/urlinfo/1/',methods=['GET'])defsearch(URL):ifsomething:a=dict(message="everythingisgood"resp=jsonify(a)returnrespelse:a=dict(error="problem")returnjsonify(a)我正在使用curl它curlhttp://127.0.0.1:5000/urlinfo/1/'https://www.youtube.com/'它以json格式返回所需的输出。我为它写了一个单元测试impor

python - 导入错误 : cannot import name 'transfer_markers' when testing with pytest

当我通过运行以下命令为我的python项目运行测试时:pythonsetup.pytest(or)pytestproject_name我收到以下错误:.../project_name/.eggs/pytest_asyncio-0.9.0-py3.6.egg/pytest_asyncio/plugin.py",line8,infrom_pytest.pythonimporttransfer_markersImportError:cannotimportname'transfer_markers' 最佳答案 当我在网上查找时,几乎没有任

Python 列表理解 : test function return

有没有办法在列表(或字典)理解中测试函数的返回?我想避免这样写:lst=[]forxinrange(10):bar=foo(x)ifbar:lst.append(bar)并改用列表理解。显然,我不想写:[foo(x)forxinrange(10)iffoo(x)]所以呢?[foo(x)forxinrange(10)if???] 最佳答案 怎么样filter(None,map(foo,range(10)))如果您不想保留中间列表,请将map()替换为itertools.imap().和itertools.ifilter(),整个东西可

python - py.test : specifying python_files in the command line

我想在pytest中设置参数python_files。文档说你需要把它放在一个配置文件中,但我想把它作为调用py.test的一部分包含在命令行中,所以我不必添加那个配置文件。这可能吗? 最佳答案 不可能像现在(2.8)那样开箱即用的pytest。pytestparser了解命令行选项(addoption/getoption)和配置文件值(addini/getini),但它们完全不同。可能可以编写一个插件来添加python_files(或任何其他ini值)作为命令行选项。但只添加一个conftest文件肯定是更简单的选择。

python - 运行 py.test 时的 .coveragerc 文件位置

我正在尝试让pytest运行以处理coveragerc文件。我的.coveragerc文件中的设置没有被使用,所以我猜这个文件根本没有被使用。在下面查看我的项目结构和pytest调用!我做错了什么?项目:basepath/lib/basepath/.coveragercbasepath/test/test_libbasepath/test/run.py我从virtualenv调用test/run.pybasepath$pythontest/run.py运行.pyimportpytestpytest.main('test/test_lib-v--cov-reportxml--covlib

python - DB-Connections 类作为 Python 中的单例

所以在python中有很多人讨厌单例。我通常认为拥有一个单例通常不好,但是那些有副作用的东西呢,比如使用/查询数据库?为什么我要为每个简单的查询创建一个新的实例,当我可以重新使用已经建立的现有连接时?什么是pythonic方法/替代方法?谢谢! 最佳答案 通常,您有某种对象代表使用数据库的事物(例如,MyWebServer的实例),并且您使数据库连接成为该对象的成员。如果您改为将所有逻辑都放在某种函数中,请将连接设为该函数的本地连接。(这在许多其他语言中不太常见,但在Python中,通常有很好的方法将多阶段有状态工作包装在单个生成器

python - 将命令行参数作为参数传递给 py.test fixture

诚然,这不是开始的最佳方式,更重要的是,fixture参数已解析,即Options.get_option()在其他一切之前被调用。建议和建议将不胜感激。来自config.pyclassOptions(object):option=None@classmethoddefget_option(cls):returncls.option来自conftest.py@pytest.yield_fixture(scope='session',autouse=True)defsession_setup():Options.option=pytest.config.getoption('--remot

python - 类型错误 : urlopen() got multiple values for keyword argument 'body' while executing tests through Selenium and Python on Kubuntu 14. 04

我正在尝试在Kubuntu14.04上用python运行selenium。我在尝试使用chromedriver或geckodriver时收到此错误消息,两者都是相同的错误。Traceback(mostrecentcalllast):File"vse.py",line15,indriver=webdriver.Chrome(chrome_options=options,executable_path=r'/root/Desktop/chromedriver')File"/usr/local/lib/python3.4/dist-packages/selenium/webdriver/ch

python - 使用 `django.test.client` 时 400 错误请求

我认为我缺少基本设置。我正在尝试使用Django的测试框架测试我的API。在shell中,我尝试:fromdjango.testimportClientc=Client()r=c.get('/')我得到一个400BadRequest,这不是预期的输出。从命令行使用简单的curl:curlhttp://localhost我得到了预期的输出:{"detail":"Authenticationcredentials...我是否遗漏了一些基本的东西? 最佳答案 您可以在DEBUG为False时使用测试客户端,您只需将“testserver”