Skip to content

Commit 1ea29c7

Browse files
committed
Convert unittest to pytest
Needs local imports to work. Conversion done with unittests2pytests and a couple regex
1 parent ba7794b commit 1ea29c7

37 files changed

+5937
-5745
lines changed

src/tests/_compat.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
unicode = str
3434

3535
# from nowhere import Nothing
36-
cmp = lambda a, b: (a > b) - (a < b) # No Py3 equivalent
36+
cmp = lambda a, b: (a > b) - (a < b) # No PY3 equivalent
3737
map = map
3838
range = range
3939
zip = zip

src/tests/conftest.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# -*- coding: utf-8 -*-
2+
# TODO: move tests one out of src. Pythonnet doesn't run...
3+
4+
"""Helpers for testing."""
5+
6+
import io
7+
import os
8+
import sys
9+
10+
import pytest
11+
import clr
12+
13+
sys.path.append('C:/testdir/')
14+
clr.AddReference("Python.Test")
15+
clr.AddReference("System.Collections")
16+
clr.AddReference("System.Data")
17+
18+
DIR_PATH = os.path.dirname(__file__)
19+
FILES_DIR = os.path.join(DIR_PATH, 'files')
20+
21+
22+
@pytest.fixture()
23+
def filepath():
24+
"""Returns full file path for test files."""
25+
26+
def make_filepath(filename):
27+
# http://stackoverflow.com/questions/18011902/parameter-to-a-fixture
28+
# Alternate solution is to use paramtrization `inderect=True`
29+
# http://stackoverflow.com/a/33879151
30+
# Syntax is noisy and requires specific variable names
31+
return os.path.join(FILES_DIR, filename)
32+
33+
return make_filepath
34+
35+
36+
@pytest.fixture()
37+
def load_file(filepath):
38+
"""Opens filename with encoding and return its contents."""
39+
40+
def make_load_file(filename, encoding='utf-8'):
41+
# http://stackoverflow.com/questions/18011902/parameter-to-a-fixture
42+
# Alternate solution is to use paramtrization `inderect=True`
43+
# http://stackoverflow.com/a/33879151
44+
# Syntax is noisy and requires specific variable names
45+
# And seems to be limited to only 1 argument.
46+
with io.open(filepath(filename), encoding=encoding) as f:
47+
return f.read().strip()
48+
49+
return make_load_file
50+
51+
52+
@pytest.fixture()
53+
def get_stream(filepath):
54+
def make_stream(filename, encoding='utf-8'):
55+
return io.open(filepath(filename), encoding=encoding)
56+
57+
return make_stream

src/tests/leaktest.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010

1111
import System
1212

13-
from _compat import range
14-
from utils import (CallableHandler, ClassMethodHandler, GenericHandler,
15-
HelloClass, StaticMethodHandler, VarCallableHandler,
16-
VariableArgsHandler, hello_func)
13+
from ._compat import range
14+
from .utils import (CallableHandler, ClassMethodHandler, GenericHandler,
15+
HelloClass, StaticMethodHandler, VarCallableHandler,
16+
VariableArgsHandler, hello_func)
1717

1818

1919
class LeakTest(object):

src/tests/profile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import time
1515

1616
import runtests
17-
from _compat import range
17+
from ._compat import range
1818

1919

2020
def main():

src/tests/runtests.py

Lines changed: 7 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@
55

66
from __future__ import print_function
77

8-
import os
98
import sys
10-
import unittest
9+
import pytest
1110

12-
from _compat import input
11+
from ._compat import input
1312

1413
try:
1514
import System
@@ -22,63 +21,16 @@
2221
clr.AddReference("System.Data")
2322
clr.AddReference("System.Management")
2423

25-
test_modules = (
26-
# has to be first test before other module import clr
27-
'test_sysargv',
28-
24+
25+
def main(verbosity=1):
2926
# test_module passes on its own, but not here if
3027
# other test modules that import System.Windows.Forms
3128
# run first. They must not do module level import/AddReference()
3229
# of the System.Windows.Forms namespace.
33-
'test_module',
34-
35-
'test_suite',
36-
'test_event',
37-
'test_constructors',
38-
'test_enum',
39-
'test_method',
40-
'test_exceptions',
41-
'test_compat',
42-
'test_generic',
43-
'test_conversion',
44-
'test_class',
45-
'test_interface',
46-
'test_field',
47-
'test_property',
48-
'test_indexer',
49-
'test_delegate',
50-
'test_array',
51-
'test_thread',
52-
'test_docstring',
53-
54-
# FIXME: Has tests that are being skipped.
55-
'test_engine',
56-
57-
# FIXME: Has tests that are being skipped.
58-
'test_subclass',
59-
)
60-
61-
62-
def remove_pyc():
63-
path = os.path.dirname(os.path.abspath(__file__))
64-
for name in test_modules:
65-
pyc = os.path.join(path, "{0}.pyc".format(name))
66-
if os.path.isfile(pyc):
67-
os.unlink(pyc)
68-
69-
70-
def main(verbosity=1):
71-
remove_pyc()
72-
73-
suite = unittest.TestSuite()
74-
75-
for name in test_modules:
76-
module = __import__(name)
77-
suite.addTests((module.test_suite(),))
7830

79-
result = unittest.TextTestRunner(verbosity=verbosity).run(suite)
80-
if not result.wasSuccessful():
81-
raise Exception("Tests failed")
31+
# FIXME: test_engine has tests that are being skipped.
32+
# FIXME: test_subclass has tests that are being skipped.
33+
pytest.main()
8234

8335

8436
if __name__ == '__main__':

src/tests/stress.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
import threading
1818
import time
1919

20-
from _compat import range, thread
21-
from utils import dprint
20+
from ._compat import range, thread
21+
from .utils import dprint
2222

2323

2424
class StressTest(object):

src/tests/stresstest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import unittest
1212
# import pdb
1313

14-
from _compat import range
14+
from ._compat import range
1515

1616
try:
1717
import System

0 commit comments

Comments
 (0)