Question

using nose to test a flask app. I'm trying to use the with_setup decorator to make my test DRY, without having to repeat the setup every test function. but it doesn't seem to run the @with_setup phase. As the docs state I'm using it with test functions and not test classes. some code:

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

for example if I create the fixture dict inside the @with_setup block, instead of inside the specific test method (and in everyone of them) I'll get a NameError (or something similar)

I guess I'm missing something, just not sure what. Thanks for the help!

Was it helpful?

Solution

The issue is that the names RUN_FOLDER and fixture scoped to the function setup and so will not be available to test_scrape_wellformed_html. If you look at the code for with_setup you will see that it does not do anything to alter the run function's environment.

In order to do what you want to do you need to make your fixtures global variables:

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top