8000 Clean-up Tests by vmuriart · Pull Request #329 · pythonnet/pythonnet · GitHub
[go: up one dir, main page]

Skip to content

Clean-up Tests #329

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

Closed
wants to merge 14 commits into from
Prev Previous commit
Next Next commit
Use unittest.skip(...)
  • Loading branch information
vmuriart committed Jan 31, 2017
commit b4e8d97875a7837bc19da22386ba199a2ca970a9
11 changes: 6 additions & 5 deletions src/tests/runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@
except ImportError:
print("Load clr import hook")
import clr

clr.AddReference("Python.Test")
clr.AddReference("System.Collections")
clr.AddReference("System.Data")
clr.AddReference("System.Management")

test_modules = (
# Passes on its own, but not here if
# 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.
Expand All @@ -47,11 +48,11 @@
'test_thread',
'test_docstring',

# FIXME: Fails due to unhandled exception
# 'test_engine',
# FIXME: Has tests that are being skipped.
'test_engine',

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


Expand Down
3 changes: 2 additions & 1 deletion src/tests/test_engine.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# -*- coding: utf-8 -*-
# FIXME: This test module fails due to unhandled exceptions

import sys
import unittest
Expand All @@ -20,12 +19,14 @@ def test_multiple_calls_to_initialize(self):
except BaseException:
self.fail("Initialize() raise an exception.")

@unittest.skip(reason="FIXME: test crashes")
def test_import_module(self):
"""Test module import."""
m = PythonEngine.ImportModule("sys")
n = m.GetAttr("__name__")
self.assertTrue(n.AsManagedObject(System.String) == "sys")

@unittest.skip(reason="FIXME: test freezes")
def test_run_string(self):
"""Test the RunString method."""
PythonEngine.AcquireLock()
Expand Down
31 changes: 15 additions & 16 deletions src/tests/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,23 +306,22 @@ def test_pickling_exceptions(self):

self.assertEqual(exc.args, loaded.args)

@unittest.skipIf(PY2, "__cause__ isn't implemented in PY2")
def test_chained_exceptions(self):
# __cause__ is py3 only
if PY3:
from Python.Test import ExceptionTest

try:
ExceptionTest.ThrowChainedExceptions()
except Exception as exc:
msgs = ("Outer exception",
"Inner exception",
"Innermost exception",)
for msg in msgs:
self.assertEqual(exc.Message, msg)
self.assertEqual(exc.__cause__, exc.InnerException)
exc = exc.__cause__
else:
self.fail("Test should raise an exception")
from Python.Test import ExceptionTest

with self.assertRaises(Exception) as cm:
ExceptionTest.ThrowChainedExceptions()

exc = cm.exception

msgs = ("Outer exception",
"Inner exception",
"Innermost exception",)
for msg in msgs:
self.assertEqual(exc.Message, msg)
self.assertEqual(exc.__cause__, exc.InnerException)
exc = exc.__cause__


def test_suite():
Expand Down
107 changes: 68 additions & 39 deletions src/tests/test_subclass.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# -*- coding: utf-8 -*-
# FIXME: This test module fails on Linux
# FIXME: This test module randomly passes/fails even if all tests are skipped.
# Something fishy is going on with the Test fixtures. Behavior seen on CI on
# both Linux and Windows
# TODO: Remove delay of class creations. Adding SetUp/TearDown may help

import unittest

Expand All @@ -11,64 +14,80 @@
from _compat import range


class InterfaceTestClass(IInterfaceTest):
"""class that implements the test interface"""
__namespace__ = "Python.Test"
def interface_test_class_fixture():
"""Delay creation of class until test starts."""

def foo(self):
return "InterfaceTestClass"
class InterfaceTestClass(IInterfaceTest):
"""class that implements the test interface"""
__namespace__ = "Python.Test"

def bar(self, x, i):
return "/".join([x] * i)
def foo(self):
return "InterfaceTestClass"

def bar(self, x, i):
return "/".join([x] * i)

class DerivedClass(SubClassTest):
"""class that derives from a class deriving from IInterfaceTest"""
__namespace__ = "Python.Test"
return InterfaceTestClass

def foo(self):
return "DerivedClass"

def base_foo(self):
return SubClassTest.foo(self)
def derived_class_fixture():
"""Delay creation of class until test starts."""

def super_foo(self):
return super(DerivedClass, self).foo()
class DerivedClass(SubClassTest):
"""class that derives from a class deriving from IInterfaceTest"""
__namespace__ = "Python.Test"

def bar(self, x, i):
return "_".join([x] * i)
def foo(self):
return "DerivedClass"

def return_list(self):
l = List[str]()
l.Add("A")
l.Add("B")
l.Add("C")
return l
def base_foo(self):
return SubClassTest.foo(self)

def super_foo(self):
return super(DerivedClass, self).foo()

class DerivedEventTest(IInterfaceTest):
"""class that implements IInterfaceTest.TestEvent"""
__namespace__ = "Python.Test"
def bar(self, x, i):
return "_".join([x] * i)

def __init__(self):
self.event_handlers = []
def return_list(self):
l = List[str]()
l.Add("A")
l.Add("B")
l.Add("C")
return l

# event handling
def add_TestEvent(self, handler):
self.event_handlers.append(handler)
return DerivedClass

def remove_TestEvent(self, handler):
self.event_handlers.remove(handler)

def OnTestEvent(self, value):
args = EventArgsTest(value)
for handler in self.event_handlers:
handler(self, args)
def derived_event_test_class_fixture():
"""Delay creation of class until test starts."""

class DerivedEventTest(IInterfaceTest):
"""class that implements IInterfaceTest.TestEvent"""
__namespace__ = "Python.Test"

def __init__(self):
self.event_handlers = []

# event handling
def add_TestEvent(self, handler):
self.event_handlers.append(handler)

def remove_TestEvent(self, handler):
self.event_handlers.remove(handler)

def OnTestEvent(self, value):
args = EventArgsTest(value)
for handler in self.event_handlers:
handler(self, args)

return DerivedEventTest


class SubClassTests(unittest.TestCase):
"""Test sub-classing managed types"""

@unittest.skip(reason="FIXME: test randomly pass/fails")
def test_base_class(self):
"""Test base class managed type"""
ob = SubClassTest()
Expand All @@ -80,8 +99,10 @@ def test_base_class(self):
self.assertEqual(list(ob.return_list()), ["a", "b", "c"])
self.assertEqual(list(SubClassTest.test_list(ob)), ["a", "b", "c"])

@unittest.skip(reason="FIXME: test randomly pass/fails")
def test_interface(self):
"""Test python classes can derive from C# interfaces"""
InterfaceTestClass = interface_test_class_fixture()
ob = InterfaceTestClass()
self.assertEqual(ob.foo(), "InterfaceTestClass")
self.assertEqual(FunctionsTest.test_foo(ob), "InterfaceTestClass")
Expand All @@ -91,8 +112,10 @@ def test_interface(self):
x = FunctionsTest.pass_through(ob)
self.assertEqual(id(x), id(ob))

@unittest.skip(reason="FIXME: test randomly pass/fails")
def test_derived_class(self):
"""Test python class derived from managed type"""
DerivedClass = derived_class_fixture()
ob = DerivedClass()
self.assertEqual(ob.foo(), "DerivedClass")
self.assertEqual(ob.base_foo(), "foo")
Expand All @@ -107,8 +130,10 @@ def test_derived_class(self):
x = FunctionsTest.pass_through(ob)
self.assertEqual(id(x), id(ob))

@unittest.skip(reason="FIXME: test randomly pass/fails")
def test_create_instance(self):
"""Test derived instances can be created from managed code"""
DerivedClass = derived_class_fixture()
ob = FunctionsTest.create_instance(DerivedClass)
self.assertEqual(ob.foo(), "DerivedClass")
self.assertEqual(FunctionsTest.test_foo(ob), "DerivedClass")
Expand All @@ -119,6 +144,7 @@ def test_create_instance(self):
x = FunctionsTest.pass_through(ob)
self.assertEqual(id(x), id(ob))

InterfaceTestClass = interface_test_class_fixture()
ob2 = FunctionsTest.create_instance(InterfaceTestClass)
self.assertEqual(ob2.foo(), "InterfaceTestClass")
self.assertEqual(FunctionsTest.test_foo(ob2), "InterfaceTestClass")
Expand All @@ -128,6 +154,7 @@ def test_create_instance(self):
y = FunctionsTest.pass_through(ob2)
self.assertEqual(id(y), id(ob2))

@unittest.skip(reason="FIXME: test randomly pass/fails")
def test_events(self):
class EventHandler(object):
def handler(self, x, args):
Expand All @@ -140,10 +167,12 @@ def handler(self, x, args):
self.assertEqual(FunctionsTest.test_event(x, 1), 1)
self.assertEqual(event_handler.value, 1)

InterfaceTestClass = interface_test_class_fixture()
i = InterfaceTestClass()
with self.assertRaises(System.NotImplementedException):
FunctionsTest.test_event(i, 2)

DerivedEventTest = derived_event_test_class_fixture()
d = DerivedEventTest()
d.add_TestEvent(event_handler.handler)
self.assertEqual(FunctionsTest.test_event(d, 3), 3)
Expand Down
0