mardi 19 septembre 2017

pytest not finding variables in module namespace

pytest is not finding my server variable, nor am I able to share a fixture across tests as I believe should be possible, hence the import line :

from fixture_ex import x

Where am I going wrong here?

testing_examples/fixture_ex.py :

#!/usr/bin/env python3

import logging
import pytest

logging.basicConfig(
    level=logging.DEBUG,
    format="%(asctime)s - %(levelname)s - %(message)s")
#logging.disable(logging.CRITICAL)

class X:
    def __init__(self):
        self.prices = {}

@pytest.fixture(scope="module")
def x(request):
    server = getattr("request.module", "server", "failed")
    logging.debug("server: {}".format(server))
    obj = X()
    def fin():
        print("tearing down obj")
    request.addfinalizer(fin)
    return obj

testing_examples.test_fixture_scope.py :

#!/usr/bin/env python3

from fixture_ex import x

server = "telegraph.co.uk"

def test_scope(x):
    assert x.prices == {}

Use at CL:

py.test test_fixture_scope.py -s -qq
failed
.tearing down obj

Aucun commentaire:

Enregistrer un commentaire