我用一个新的更简单的假设置重新创建了这个问题。
我有一个框架需要来自 pytest 的命令 channel 变量。此变量称为环境,但当我尝试访问该变量时,我得到一个 AttributeError:“模块”对象没有属性“配置”。
这是我的测试设置:
我知道 py.test 按以下顺序加载:
我想我遇到了一个问题,当我加载内部 conftest.py 时,我尝试导入框架。当我导入框架时,它会尝试访问 py.test 变量。这个变量,即使 pytest 已经在我的 outer-conftest.py 的 pytest_addoption() 部分中看到它,还没有准备好在 pytest 中使用。
外赛内容:
# content of conftest.py
import pytest
def pytest_addoption(parser):
print("First")
parser.addoption("--cmdopt", action="store", default="type1",
help="my option: type1 or type2")
@pytest.fixture
def cmdopt(request):
return request.config.getoption("cmdopt")
framework.py 的内容:
import pytest
class Environment:
@staticmethod
def env():
'''Determine which environment we are operating in,
if it fails - we assume dca
'''
return pytest.config.getoption('cmdopt')
class Users:
__pool = Environment.env()
内部conftest.py内容:
import pytest
from testing.framework import Environment
test_sample.py 的内容:
# content of test_sample.py
def test_answer(cmdopt):
if cmdopt == "type1":
print ("first")
elif cmdopt == "type2":
print ("second")
assert 0 # to see what was printed
我在 testing/文件夹中运行以下命令: py.test -q --cmdopt=type2
我收到以下错误:
First
Second
Traceback (most recent call last):
File "/usr/local/lib/python3.4/dist-packages/_pytest/config.py", line 513, in getconftestmodules
return self._path2confmods[path]
KeyError: local('/home/damonp/Repos/stuff/<my-name-redacted>/testing/tests')
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.4/dist-packages/_pytest/config.py", line 537, in importconftest
return self._conftestpath2mod[conftestpath]
KeyError: local('/home/damonp/Repos/stuff/<my-name-redacted>/testing/tests/conftest.py')
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.4/dist-packages/_pytest/config.py", line 543, in importconftest
mod = conftestpath.pyimport()
File "/usr/local/lib/python3.4/dist-packages/py/_path/local.py", line 641, in pyimport
__import__(modname)
File "/home/damonp/Repos/stuff/<my-name-redacted>/testing/tests/conftest.py", line 5, in <module>
from testing.framework import Environment
File "/home/damonp/Repos/stuff/<my-name-redacted>/testing/framework.py", line 14, in <module>
class Users:
File "/home/damonp/Repos/stuff/<my-name-redacted>/testing/framework.py", line 15, in Users
__pool = Environment.env()
File "/home/damonp/Repos/stuff/<my-name-redacted>/testing/framework.py", line 11, in env
return pytest.config.getoption('cmdopt')
AttributeError: 'module' object has no attribute 'config'
ERROR: could not load /home/damonp/Repos/stuff/<my-name-redacted>/testing/tests/conftest.py
是否有使用依赖于 pytest 命令行变量的外部框架的好方法?
最佳答案
理想情况下,您应该拥有从框架中为您提供对象的固定装置,例如 User(s) 实例。
这些固定装置可以 receive cmdopt fixture,然后可以将该值传递给 init 方法或工厂函数。
例如
@pytest.fixture(scope="function")
def user(cmdarg):
return Users(cmdarg, ...)
然后在你的测试中,
def test_something(user):
# do something with user object
关于python - 尝试访问命令行变量时出现 Pytest KeyError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32995122/
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
我正在用Ruby编写一个简单的程序来检查域列表是否被占用。基本上它循环遍历列表,并使用以下函数进行检查。require'rubygems'require'whois'defcheck_domain(domain)c=Whois::Client.newc.query("google.com").available?end程序不断出错(即使我在google.com中进行硬编码),并打印以下消息。鉴于该程序非常简单,我已经没有什么想法了-有什么建议吗?/Library/Ruby/Gems/1.8/gems/whois-2.0.2/lib/whois/server/adapters/base.
我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po
我想用ruby编写一个小的命令行实用程序并将其作为gem分发。我知道安装后,Guard、Sass和Thor等某些gem可以从命令行自行运行。为了让gem像二进制文件一样可用,我需要在我的gemspec中指定什么。 最佳答案 Gem::Specification.newdo|s|...s.executable='name_of_executable'...endhttp://docs.rubygems.org/read/chapter/20 关于ruby-在Ruby中编写命令行实用程序
我有一个包含模块的模型。我想在模块中覆盖模型的访问器方法。例如:classBlah这显然行不通。有什么想法可以实现吗? 最佳答案 您的代码看起来是正确的。我们正在毫无困难地使用这个确切的模式。如果我没记错的话,Rails使用#method_missing作为属性setter,因此您的模块将优先,阻止ActiveRecord的setter。如果您正在使用ActiveSupport::Concern(参见thisblogpost),那么您的实例方法需要进入一个特殊的模块:classBlah
我正在查看instance_variable_set的文档并看到给出的示例代码是这样做的:obj.instance_variable_set(:@instnc_var,"valuefortheinstancevariable")然后允许您在类的任何实例方法中以@instnc_var的形式访问该变量。我想知道为什么在@instnc_var之前需要一个冒号:。冒号有什么作用? 最佳答案 我的第一直觉是告诉你不要使用instance_variable_set除非你真的知道你用它做什么。它本质上是一种元编程工具或绕过实例变量可见性的黑客攻击
我正在使用Sequel构建一个愿望list系统。我有一个wishlists和itemstable和一个items_wishlists连接表(该名称是续集选择的名称)。items_wishlists表还有一个用于facebookid的额外列(因此我可以存储opengraph操作),这是一个NOTNULL列。我还有Wishlist和Item具有续集many_to_many关联的模型已建立。Wishlist类也有:selectmany_to_many关联的选项设置为select:[:items.*,:items_wishlists__facebook_action_id].有没有一种方法可以
我正在编写一个gem,我必须在其中fork两个启动两个webrick服务器的进程。我想通过基类的类方法启动这个服务器,因为应该只有这两个服务器在运行,而不是多个。在运行时,我想调用这两个服务器上的一些方法来更改变量。我的问题是,我无法通过基类的类方法访问fork的实例变量。此外,我不能在我的基类中使用线程,因为在幕后我正在使用另一个不是线程安全的库。所以我必须将每个服务器派生到它自己的进程。我用类变量试过了,比如@@server。但是当我试图通过基类访问这个变量时,它是nil。我读到在Ruby中不可能在分支之间共享类变量,对吗?那么,还有其他解决办法吗?我考虑过使用单例,但我不确定这是
我是一个Rails初学者,但我想从我的RailsView(html.haml文件)中查看Ruby变量的内容。我试图在ruby中打印出变量(认为它会在终端中出现),但没有得到任何结果。有什么建议吗?我知道Rails调试器,但更喜欢使用inspect来打印我的变量。 最佳答案 您可以在View中使用puts方法将信息输出到服务器控制台。您应该能够在View中的任何位置使用Haml执行以下操作:-puts@my_variable.inspect 关于ruby-on-rails-如何在我的R