2013-12-14 83 views
1

使用鼻子測試燒瓶應用程序。我試圖用with_setup修飾器使我的測試乾燥,而不必重複設置每個測試功能。但它似乎並沒有運行@with_setup階段。作爲文檔state我用它與測試功能,而不是測試類。一些代碼:鼻子@with_setup設置無法在測試中使用

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塊內的夾具字典,而不是具體的測試方法中(以及在他們每個人)我會得到一個NameError(或類似的東西)

我想我錯過了什麼,只是不知道是什麼。 感謝您的幫助!

回答

1

問題在於名稱RUN_FOLDERfixture的作用域爲功能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 
+0

很好的文檔都不清楚這個..讓檢查,我會更新 – alonisser

+0

的感謝!解決了這個問題。我希望文檔在這方面更具體 – alonisser