Skip to content

Instantly share code, notes, and snippets.

@danilobellini
Created November 14, 2012 04:03
Show Gist options
  • Save danilobellini/4070188 to your computer and use it in GitHub Desktop.
Save danilobellini/4070188 to your computer and use it in GitHub Desktop.
Strange behaviour with pytest + warnings
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on Wed Nov 14 01:39:53 2012
@author: Danilo J. S. Bellini
"""
from warnings import warn, catch_warnings
import pytest
class Warner(object):
def __init__(self):
self.data = [1, 2]
def pop(self):
return self.data.pop()
def bad(self):
""" Get warned, bro! """
if self.data != []:
warn("Blah", Warning)
print "I was here!"
def test():
bros = Warner()
bros.pop()
with catch_warnings(record=True) as warn_list:
bros.bad()
assert bros.data != []
assert warn_list != []
# Put the code below inside a
# if __name__ == "__main__":
# and the problem get solved...why?
pytest.main('"{0}"'.format(__file__))
============================= test session starts ==============================
platform linux2 -- Python 2.7.3 -- pytest-2.2.4
collecting ... collected 1 items
warnbug.py F
=================================== FAILURES ===================================
_____________________________________ test _____________________________________
def test():
bros = Warner()
bros.pop()
with catch_warnings(record=True) as warn_list:
bros.bad()
assert bros.data != []
> assert warn_list != []
E assert [] != []
warnbug.py:34: AssertionError
------------------------------- Captured stdout --------------------------------
I was here!
=========================== 1 failed in 0.02 seconds ===========================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment