mercredi 30 mars 2016

Using nose2's such DSL to import a layer from another module

So I have a little issue using Nose2's such DSL.

I am writing a backend library that makes http requests, I need to mock out the backend rest api to test the functionality of my code, fairly standard so far.

So I thought I could make use of a nose2 layer, which works fine, as long as the layer class is defined in the same module. However as this library is going to be integrated into another rest api, I want to be able to define the mock layer in a seperate module and import it, I can then use the same layer in the api's unit tests.

However pythons scoping has scuppered my plans, as the it variable is no longer available.

Here's some example code:

In project/mocks/layers.py

class NewLayer(object):
    @classmethod
    def setUp(cls):
        it.config = True

In test/test_code.py:

from nose2.tools import such
from project.mocks.layers import NewLayer

with such.A('Test project') as it:
    with it.having('A new layer')
        it.uses(NewLayer)

        it.should('pass a test')
        def test_pass(case):
            assert it.config

When this gets run I end up with an error in the Layer suite

NameError: global name 'it' is not defined

If I define the NewLayer class in the test it will work fine. eg

from nose2.tools import such 

class NewLayer(object):
    @classmethod
    def setUp(cls):
        it.config = True

with such.A('Test project') as it:
    with it.having('A new layer')
        it.uses(NewLayer)

        it.should('pass a test')
        def test_pass(case):
            assert it.config

Is there a way of passing the it Scenario instance into the NewLayer?

Aucun commentaire:

Enregistrer un commentaire