Skip to content

Instantly share code, notes, and snippets.

@schmir
Created December 20, 2012 15:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save schmir/4345862 to your computer and use it in GitHub Desktop.
Save schmir/4345862 to your computer and use it in GitHub Desktop.
#! /usr/bin/env py.test
import pytest
class Client(object):
def __init__(self, url):
self.usable = True
self.url = url
def __repr__(self):
return "Client %r" % self.__dict__
@pytest.fixture(params=["amqp://"])
def amqp_url(request):
"""the URL to a running rabbitmq instance"""
return request.param
@pytest.fixture
def qname(request, client):
def cleanup():
print "CLEANUP QNAME", client
assert client.usable
request.addfinalizer(cleanup)
return "foo"
@pytest.fixture
def client(request, amqp_url):
"""a connected puka.Client instance"""
retval = Client(amqp_url)
def cleanup():
retval.usable = False
print "CLEANUP CLIENT"
request.addfinalizer(cleanup)
return retval
def test_baz(client, qname):
print "hello", client
# Output is something like:
# [dev] ~/ % ./pytest-cleanup-order.py
# ============================== test session starts ===============================
# platform linux2 -- Python 2.7.3 -- pytest-2.3.4
# plugins: xdist
# collected 1 items
# pytest-cleanup-order.py .E
# ===================================== ERRORS =====================================
# _____________________ ERROR at teardown of test_baz[amqp://] _____________________
# def cleanup():
# print "CLEANUP QNAME", client
# > assert client.usable
# E assert Client {'url': 'amqp://', 'usable': False}.usable
# pytest-cleanup-order.py:21: AssertionError
# -------------------------------- Captured stdout ---------------------------------
# hello Client {'url': 'amqp://', 'usable': True}
# CLEANUP CLIENT
# CLEANUP QNAME Client {'url': 'amqp://', 'usable': False}
# ======================= 1 passed, 1 error in 0.01 seconds ========================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment