Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save bobsilverberg/3801248 to your computer and use it in GitHub Desktop.

Select an option

Save bobsilverberg/3801248 to your computer and use it in GitHub Desktop.
Examples of pytest, especially funcargs
# tb==native
Traceback (most recent call last):
File "/work/blaugher/workspace/Review/GFESuite/tests/unit/formatters/test_Precis.py",
line 882, in test_lowDetail12hourWxBrackets
assert 'morningZ' in words
AssertionError: assert 'morningZ' in 'morning shower or two'
# tb==short
tests/unit/formatters/test_Precis.py:882: in test_lowDetail12hourWxBrackets
> assert 'morningZ' in words
E assert 'morningZ' in 'morning shower or two'
# tb==long
mockAccessor =
def test_lowDetail12hourWxBrackets(mockAccessor):
"""
Initial 6 hours of wx is correctly being washed out to the first 12 hours.
"""
mockAccessor.mockGrids([
Flat("Sky", 0, 24, 0),
Flat("Wx", 0, 6, "Sct:SH:m::"),
Flat("Wx", 6, 24, "NoWx"),
])
_icon, words = precisIconWords(mockAccessor, period=6, detail='low')
assert 'early' not in words
> assert 'morningZ' in words
E assert 'morningZ' in 'morning shower or two'
tests/unit/formatters/test_Precis.py:882: AssertionError
def pytest_namespace():
"""Make unittest assert methods available.
This is useful for things such floating point checks with assertAlmostEqual.
"""
import unittest
class Dummy(unittest.TestCase):
def dummy(self):
pass
obj = Dummy('dummy')
names = {name: member
for name, member in inspect.getmembers(obj)
if name.startswith('assert') and '_' not in name}
return names
# test file
def test_kn2kmh():
py.test.assertAlmostEqual(UnitConvertor.kn2kmh(10), 18.52, places=4)
def pytest_addoption(parser):
"""pytest hook that adds a GFE specific option.
"""
# Add options.
group = parser.getgroup('graphical forecast editor options')
group.addoption('--winpdb', dest='usewinpdb', action='store_true', default=False,
help=('start the WinPDB Python debugger before calling each test function. '
'Suggest only using this with a single test at a time (i.e. -k .'))
def pytest_configure(config):
# Only do these if this process is the master.
if not hasattr(config, 'slaveinput'):
# Activate winpdb plugin if appropriate.
if config.getvalue("usewinpdb"):
config.pluginmanager.register(WinPdbInvoke(), 'winpdb')
class WinPdbInvoke:
def __init__(self):
print "initialising winpdb invoker"
def pytest_pyfunc_call(self, pyfuncitem):
import rpdb2
rpdb2.start_embedded_debugger('0')
# then run: py.test -k test_some_specific_thing --winpdb
# SKIP
# inside a test, if you need to check something after the environment
# has been loaded
if not config.get('ifpServer.allowOfficalWrites'):
py.test.skip('Official DB writes are not allowed.')
# decorators:
import sys
win32only = pytest.mark.skipif("sys.platform != 'win32'")
@win32only
def test_foo():
....
@py.test.mark.skipif('True')
def test_foo1():
print "foo1"
@py.test.mark.skipif('False')
def test_foo2():
print "foo2"
def test_foo3():
py.test.skip('inside skip')
print "foo3"
# XFAIL
@py.test.mark.xfail
def test_foo4():
assert False
@py.test.mark.xfail(reason='This is a bad idea')
def test_foo5():
assert False
@py.test.mark.xfail(reason='Maybe this was a bad idea once')
def test_foo6():
assert True
def test_foo7():
# force test to be recorded as an xfail,
# even if it would otherwise pass
py.test.xfail()
assert True
# output:
test_foo4 xfail
test_foo5 xfail
test_foo6 XPASS
test_foo7 xfail
# with --runxfail:
test_foo4 FAILED
test_foo5 FAILED
test_foo6 PASSED
test_foo7 FAILED
# plus tracebacks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment