Skip to content

Instantly share code, notes, and snippets.

View nicoddemus's full-sized avatar

Bruno Oliveira nicoddemus

View GitHub Profile
@nicoddemus
nicoddemus / runtests.py
Last active August 29, 2015 14:04
How to embed `pytest.main` into a frozen executable created by cx_freeze
import pytest, sys
sys.exit(pytest.main())
@nicoddemus
nicoddemus / conftest.py
Created March 14, 2015 13:22
Expect conftest with small modification
# original from: http://pythontesting.net/framework/pytest/pytest-expect-plugin-1
import pytest
import inspect
import os.path
@pytest.fixture
def expect(request):
def do_expect(expr, msg=''):
if not expr:
_log_failure(request.node, msg)
@nicoddemus
nicoddemus / image_diff.py
Created March 25, 2015 23:18
Image diff fixture (draft)
from PIL import ImageChops, ImageDraw, Image
import pytest
import os
import py.path
import math
import operator
def rms_diff(im1, im2):
"""Calculate the root-mean-square difference between two images
Taken from: http://snipplr.com/view/757/compare-two-pil-images-in-python/
@nicoddemus
nicoddemus / gist:d30c616122e30c938b6b
Created April 27, 2015 17:08
Test instance deletion (pytest runner, both pytest style class and xUnit)
from __future__ import unicode_literals
import weakref
from coilib50 import unittest
class Test(unittest.TestCase):
_TESTS = []
def test_01(self):
Test._TESTS.append(weakref.ref(self))
@nicoddemus
nicoddemus / ren_pytest_prefix.py
Last active August 29, 2015 14:22
Rename all tests from "pytest_" to "test_", including data directories
import os
import fnmatch
import sys
directory = sys.argv[1]
for root, dirs, names in os.walk(sys.argv[1]):
for name in names:
if fnmatch.fnmatch(name, 'pytest_*.py') and os.path.basename(root) == '_tests':
source = os.path.join(root, name)
@nicoddemus
nicoddemus / close_issues.py
Created June 16, 2015 21:00
Close bitbucket issues for pytest repository, after migrating to GitHub
import requests
auth = ('nicoddemus', 'XXXX')
for issue_id in xrange(1, 768+1):
url = 'https://bitbucket.org/api/1.0/repositories/{accountname}/{repo_slug}/issues/{issue_id}/comments'
url = url.format(accountname='pytest-dev', repo_slug='pytest', issue_id=issue_id)
print '-' *10, issue_id
data = {
"content": "This issue has been moved to GitHub: https://github.com/pytest-dev/pytest/issues/{issue_id}".format(issue_id=issue_id),
}
@nicoddemus
nicoddemus / conftest.py
Created September 29, 2015 22:50
load_tests example fixed
import types
import unittest
from _pytest.unittest import TestCaseFunction
import pytest
class LoadTestsSuiteCollector(pytest.Collector):
def __init__(self, name, parent, suite):
@nicoddemus
nicoddemus / kw.py
Last active November 7, 2015 21:31
Handling keyword arguments default values
# python 2
def foo(**kwargs):
x = kwargs.pop('x', 10)
y = kwargs.pop('y', 'no value')
if kwargs:
raise TypeError('unknown keyword arguments: %s' % kwargs.keys())
print x, y
@nicoddemus
nicoddemus / warnings.py
Created November 26, 2015 17:42
warnings snippets
# Code typically replaced by _warnings
def warn(message, category=None, stacklevel=1):
"""Issue a warning, or maybe ignore it or raise an exception."""
# Check if message is already a Warning object
if isinstance(message, Warning):
category = message.__class__
# Check category argument
if category is None:
category = UserWarning
if not (isinstance(category, type) and issubclass(category, Warning)):
@nicoddemus
nicoddemus / 2.9.0.rst
Last active December 22, 2015 22:19
pytest changelog sample

2.9.0.dev

  • New pytest.mark.skip mark, which unconditional skips marked tests. Thanks @MichaelAquilina for the complete PR (#1040).
  • Fix #680: the -s and -c options should now work under xdist; Config.fromdictargs now represents its input much more faithfully. Thanks to @bukzor for the complete PR.