public
Created

  • Download Gist
pytest-cleanup-order.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
#! /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 ========================

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.