用鼻子测试烧瓶应用程序。我正在尝试使用with_setup
装饰器来进行测试DRY,而不必重复设置每个测试功能。但它似乎没有运行@with_setup阶段。作为文档state我将它与测试函数一起使用而不是测试类。一些代码:
from flask import *
from app import app
from nose.tools import eq_, assert_true
from nose import with_setup
testapp = app.test_client()
def setup():
app.config['TESTING'] = True
RUNNING_LOCAL = True
RUN_FOLDER = os.path.dirname(os.path.realpath(__file__))
fixture = {'html_hash':'aaaa'} #mocking the hash
def teardown():
app.config['TESTING'] = False
RUNNING_LOCAL = False
@with_setup(setup, teardown)
def test_scrape_wellformed_html():
#RUN_FOLDER = os.path.dirname(os.path.realpath(__file__)) #if it is here instead of inside @with_setup the code works..
#fixture = {'html_hash':'aaaa'} #mocking the hash #if it is here the code works
fixture['gush_id'] = 'current.fixed'
data = scrape_gush(fixture, RUN_FOLDER)
various assertions
例如,如果我在@with_setup块中创建fixture dict,而不是在特定的测试方法内(并且在每个人中),我将得到一个NameError(或类似的东西)
我想我错过了什么,只是不确定是什么。 谢谢你的帮助!
答案 0 :(得分:1)
问题是名称RUN_FOLDER
和fixture
作用于setup
函数,因此test_scrape_wellformed_html
无效。如果你看一下the code for with_setup
,你会发现它没有做任何改变运行功能环境的事情。
为了做你想做的事,你需要让你的灯具全局变量:
testapp = app.test_client()
RUN_FOLDER = os.path.dirname(os.path.realpath(__file__))
fixture = None
def setup():
global fixture
app.config['TESTING'] = True
fixture = {'html_hash':'aaaa'} #mocking the hash
def teardown():
global fixture
app.config['TESTING'] = False
fixture = None
@with_setup(setup, teardown)
def test_scrape_wellformed_html():
# run test here