我有三個文件;一個包含測試,一個包含數據,另一個包含運行測試的說明。試圖傳遞一個函數字典在Python中使用單獨的文件數據時失敗
base.py
#!/usr/bin/env python
class Base(object):
def __init__(self):
pass
def is_true(self):
return True
datadrv.py
def pytest_generate_tests(metafunc):
"""
Parse the data provided in scenarios.
"""
idlist = []
argvalues = []
for scenario in metafunc.cls.scenarios:
idlist.append(scenario[0])
items = scenario[1].items()
argnames = [x[0] for x in items]
argvalues.append(([x[1] for x in items]))
metafunc.parametrize(argnames, argvalues, ids=idlist)
###
# EDIT BELOW
# ADD NEW SCENARIOS
###
scenario1 = ('ACME_Manage_Keys', { 'org': 'ACME_Corporation',
'perm_name': 'ManageAcmeCorp',
'resource': 'activation_keys',
'verbs': ('manage_all',),
'allowed': {"test1": is_true}})
test_execute.py
#!/usr/bin/env python
from lib.base import Base
import pytest
from unittestzero import Assert
from data.datadrv import *
class TestDictionarySupport(object):
scenarios = [scenario1]
def test_datadriven_support(self,
org,
perm_name,
resource,
verbs,
allowed):
base = Base()
functionToCall = allowed['test1']
Assert.true(base.functionToCall())
""" THIS WORKS, uncomment the block to see """
"""
Assert.true(base.is_true())
"""
我的最終目標是讓Assert.true(base.is_true())
但是每次我都做嘗試已導致TypeErrors或NameErrors。我在這裏錯過什麼或做錯了什麼?
重現以上只需創建3個目錄;庫,數據和測試。測試中放置test_execute.py,數據放置datadrv.py,放置base.py。你需要pytest和unittestzero來運行。從根目錄可以運行py.test。
注:當我有這麼多的工作,我將有一個for循環和多次測試,以便functionToCall = allowed['test1']
將成爲像functionToCall = key[value]
而且,我的出發參考點是http://code.activestate.com/recipes/65126-dictionary-of-methodsfunctions/。
謝謝你,錯誤如下:
注意錯誤的做法不同嘗試。
'allowed': {'test1': Base.is_true}})
隨着: _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ ___ ERROR收集測試/ test_execute.py _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ ___ 測試/ test_execute.py:6:
from data.datadrv import * data/datadrv.py:22: in 'allowed': {'test1': Base.is_true}}) E NameError: name 'Base' is not defined ==================================================== 1 error in 0.02 seconds =====================================================
隨着'allowed': {'test1': base.is_true}})
: 測試/ test_execute.py: 6:
from data.datadrv import * data/datadrv.py:22: in 'allowed': {'test1': base.is_true}}) E NameError: name 'base' is not defined
隨着Assert.true(base.functionToCall())
:
tests/test_execute。吡啶:6:
from data.datadrv import * data/datadrv.py:22: in 'allowed': {'test1': is_true}}) E NameError: name 'is_true' is not defined
凡'Assert'定義? – rubik
發佈您的錯誤消息和回溯 –
更新與適當的導入。 – Eric