Skip to content

Migrate to Pytest #368

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
sudo: false

# language: csharp installs mono/dotnet but has limited python.
language: python
python:
- 2.7
Expand Down Expand Up @@ -29,12 +30,13 @@ addons:
- ca-certificates-mono

install:
- pip install pycparser coverage codecov
- pip install --upgrade pycparser coverage codecov pytest
# setup.py install works too, but need to deal w Python.test then
- coverage run setup.py build_ext --inplace

script:
- export PYTHONPATH=`pwd`:$PYTHONPATH
- python src/tests/runtests.py
- python -m pytest
# - mono ./packages/NUnit.*/tools/nunit-console.exe src/embed_tests/bin/Python.EmbeddingTest.dll

after_success:
Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ init:

install:
# install for wheels & coverage
- pip install --upgrade pip wheel coverage codecov
- pip install --upgrade pip wheel coverage codecov pytest

# Install OpenCover. Can't put on packages.config; not Linux/Mono compatible
- .\tools\nuget\nuget.exe install OpenCover -OutputDirectory packages
Expand Down
2 changes: 1 addition & 1 deletion ci/appveyor_run_tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ $RUNTIME_DIR = ".\src\runtime\bin\"
# why `2>&1 | %{ "$_" }`? see: http://stackoverflow.com/a/20950421/5208670
Write-Host ("Starting Python tests") -ForegroundColor "Green"
.$OPENCOVER -register:user -searchdirs:"$RUNTIME_DIR" -output:py.coverage `
-target:"$PY" -targetargs:src\tests\runtests.py `
-target:"$PY" -targetargs:"-m pytest" `
-returntargetcode `
2>&1 | %{ "$_" }
$PYTHON_STATUS = $LastExitCode
Expand Down
7 changes: 7 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[tool:pytest]
xfail_strict = True

[check-manifest]
ignore =
.github
.github/*
2 changes: 1 addition & 1 deletion src/tests/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
unicode = str

# from nowhere import Nothing
cmp = lambda a, b: (a > b) - (a < b) # No Py3 equivalent
cmp = lambda a, b: (a > b) - (a < b) # No PY3 equivalent
map = map
range = range
zip = zip
Expand Down
File renamed without changes.
57 changes: 57 additions & 0 deletions src/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# -*- coding: utf-8 -*-
# TODO: move tests one out of src. Pythonnet doesn't run...

"""Helpers for testing."""

import io
import os
import sys

import pytest
import clr

sys.path.append('C:/testdir/')
clr.AddReference("Python.Test")
clr.AddReference("System.Collections")
clr.AddReference("System.Data")

DIR_PATH = os.path.dirname(__file__)
FILES_DIR = os.path.join(DIR_PATH, 'files')


@pytest.fixture()
def filepath():
"""Returns full file path for test files."""

def make_filepath(filename):
# http://stackoverflow.com/questions/18011902/parameter-to-a-fixture
# Alternate solution is to use paramtrization `inderect=True`
# http://stackoverflow.com/a/33879151
# Syntax is noisy and requires specific variable names
return os.path.join(FILES_DIR, filename)

return make_filepath


@pytest.fixture()
def load_file(filepath):
"""Opens filename with encoding and return its contents."""

def make_load_file(filename, encoding='utf-8'):
# http://stackoverflow.com/questions/18011902/parameter-to-a-fixture
# Alternate solution is to use paramtrization `inderect=True`
# http://stackoverflow.com/a/33879151
# Syntax is noisy and requires specific variable names
# And seems to be limited to only 1 argument.
with io.open(filepath(filename), encoding=encoding) as f:
return f.read().strip()

return make_load_file


@pytest.fixture()
def get_stream(filepath):
def make_stream(filename, encoding='utf-8'):
return io.open(filepath(filename), encoding=encoding)

return make_stream
8 changes: 4 additions & 4 deletions src/tests/leaktest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@

import System

from _compat import range
from utils import (CallableHandler, ClassMethodHandler, GenericHandler,
HelloClass, StaticMethodHandler, VarCallableHandler,
VariableArgsHandler, hello_func)
from ._compat import range
from .utils import (CallableHandler, ClassMethodHandler, GenericHandler,
HelloClass, StaticMethodHandler, VarCallableHandler,
VariableArgsHandler, hello_func)


class LeakTest(object):
Expand Down
2 changes: 1 addition & 1 deletion src/tests/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import time

import runtests
from _compat import range
from ._compat import range


def main():
Expand Down
62 changes: 7 additions & 55 deletions src/tests/runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@

from __future__ import print_function

import os
import sys
import unittest
import pytest

from _compat import input
from ._compat import input

try:
import System
Expand All @@ -22,63 +21,16 @@
clr.AddReference("System.Data")
clr.AddReference("System.Management")

test_modules = (
# has to be first test before other module import clr
'test_sysargv',


def main(verbosity=1):
# test_module passes on its own, but not here if
# other test modules that import System.Windows.Forms
# run first. They must not do module level import/AddReference()
# of the System.Windows.Forms namespace.
'test_module',

'test_suite',
'test_event',
'test_constructors',
'test_enum',
'test_method',
'test_exceptions',
'test_compat',
'test_generic',
'test_conversion',
'test_class',
'test_interface',
'test_field',
'test_property',
'test_indexer',
'test_delegate',
'test_array',
'test_thread',
'test_docstring',

# FIXME: Has tests that are being skipped.
'test_engine',

# FIXME: Has tests that are being skipped.
'test_subclass',
)


def remove_pyc():
path = os.path.dirname(os.path.abspath(__file__))
for name in test_modules:
pyc = os.path.join(path, "{0}.pyc".format(name))
if os.path.isfile(pyc):
os.unlink(pyc)


def main(verbosity=1):
remove_pyc()

suite = unittest.TestSuite()

for name in test_modules:
module = __import__(name)
suite.addTests((module.test_suite(),))

result = unittest.TextTestRunner(verbosity=verbosity).run(suite)
if not result.wasSuccessful():
raise Exception("Tests failed")
# FIXME: test_engine has tests that are being skipped.
# FIXME: test_subclass has tests that are being skipped.
pytest.main()


if __name__ == '__main__':
Expand Down
4 changes: 2 additions & 2 deletions src/tests/stress.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
import threading
import time

from _compat import range, thread
from utils import dprint
from ._compat import range, thread
from .utils import dprint


class StressTest(object):
Expand Down
2 changes: 1 addition & 1 deletion src/tests/stresstest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import unittest
# import pdb

from _compat import range
from ._compat import range

try:
import System
Expand Down
Loading