From 595a63d6354811ab906f185801611fbe842bc385 Mon Sep 17 00:00:00 2001 From: Victor Uriarte Date: Wed, 8 Feb 2017 00:01:26 -0700 Subject: [PATCH 1/4] Add Tuple tests --- src/embed_tests/Python.EmbeddingTest.csproj | 1 + src/embed_tests/pytuple.cs | 94 +++++++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 src/embed_tests/pytuple.cs diff --git a/src/embed_tests/Python.EmbeddingTest.csproj b/src/embed_tests/Python.EmbeddingTest.csproj index 48fe2554e..e1383f996 100644 --- a/src/embed_tests/Python.EmbeddingTest.csproj +++ b/src/embed_tests/Python.EmbeddingTest.csproj @@ -81,6 +81,7 @@ + diff --git a/src/embed_tests/pytuple.cs b/src/embed_tests/pytuple.cs new file mode 100644 index 000000000..ea2319d3c --- /dev/null +++ b/src/embed_tests/pytuple.cs @@ -0,0 +1,94 @@ +using NUnit.Framework; +using Python.Runtime; + +namespace Python.EmbeddingTest +{ + public class PyTupleTest + { + [Test] + public void TestPyTupleEmpty() + { + using (Py.GIL()) + { + var t = new PyTuple(); + Assert.AreEqual(0, t.Length()); + } + } + + [Test] + [ExpectedException("Python.Runtime.PythonException")] + public void TestPyTupleInvalidAppend() + { + using (Py.GIL()) + { + PyObject s = new PyString("foo"); + var t = new PyTuple(); + t.Concat(s); + } + } + + [Test] + public void TestPyTupleValidAppend() + { + using (Py.GIL()) + { + var t0 = new PyTuple(); + var t = new PyTuple(); + t.Concat(t0); + Assert.IsNotNull(t); + Assert.IsInstanceOf(typeof(PyTuple), t); + } + } + + [Test] + public void TestPyTupleIsTupleType() + { + using (Py.GIL()) + { + var s = new PyString("foo"); + var t = new PyTuple(); + Assert.IsTrue(PyTuple.IsTupleType(t)); + Assert.IsFalse(PyTuple.IsTupleType(s)); + } + } + + [Test] + public void TestPyTupleStringConvert() + { + using (Py.GIL()) + { + PyObject s = new PyString("foo"); + PyTuple t = PyTuple.AsTuple(s); + Assert.IsNotNull(t); + Assert.IsInstanceOf(typeof(PyTuple), t); + Assert.AreEqual("f", t[0].ToString()); + Assert.AreEqual("o", t[1].ToString()); + Assert.AreEqual("o", t[2].ToString()); + } + } + + [Test] + public void TestPyTupleValidConvert() + { + using (Py.GIL()) + { + var l = new PyList(); + PyTuple t = PyTuple.AsTuple(l); + Assert.IsNotNull(t); + Assert.IsInstanceOf(typeof(PyTuple), t); + } + } + + [Test] + public void TestNewPyTupleFromPyTuple() + { + using (Py.GIL()) + { + var t0 = new PyTuple(); + var t = new PyTuple(t0); + Assert.IsNotNull(t); + Assert.IsInstanceOf(typeof(PyTuple), t); + } + } + } +} From 0e051f0b83ded0be0ba1e49a415c4d7bc5d7ae29 Mon Sep 17 00:00:00 2001 From: Victor Uriarte Date: Wed, 8 Feb 2017 10:52:10 -0700 Subject: [PATCH 2/4] Update AppVeyor comments and show when tests start. --- ci/appveyor_run_tests.ps1 | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ci/appveyor_run_tests.ps1 b/ci/appveyor_run_tests.ps1 index 3bfaf216d..300b2caa0 100644 --- a/ci/appveyor_run_tests.ps1 +++ b/ci/appveyor_run_tests.ps1 @@ -12,6 +12,7 @@ $RUNTIME_DIR = ".\src\runtime\bin\" # Run python tests with C# coverage # 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 -returntargetcode 2>&1 | %{ "$_" } $PYTHON_STATUS = $LastExitCode if ($PYTHON_STATUS -ne 0) { @@ -19,13 +20,14 @@ if ($PYTHON_STATUS -ne 0) { } # Run Embedded tests with C# coverage +Write-Host ("Starting embedded tests") -ForegroundColor "Green" .$OPENCOVER -register:user -searchdirs:"$RUNTIME_DIR" -output:cs.coverage -target:"$NUNIT" -targetargs:"$CS_TESTS" -returntargetcode $NUNIT_STATUS = $LastExitCode if ($NUNIT_STATUS -ne 0) { Write-Host "Embedded tests failed" -ForegroundColor "Red" } -# Embedded tests failing due to open issues, pass/fail only on Python exit code +# Set exit code to fail if either Python or Embedded tests failed if ($PYTHON_STATUS -ne 0 -or $NUNIT_STATUS -ne 0) { Write-Host "Tests failed" -ForegroundColor "Red" $host.SetShouldExit(1) From 9ed49e20f7597e4e29b44d0f9e5b89bb66b2e201 Mon Sep 17 00:00:00 2001 From: Victor Uriarte Date: Wed, 8 Feb 2017 12:27:35 -0700 Subject: [PATCH 3/4] Rename InitializeTest to pyinitialize Keep consistent with other test classes. --- src/embed_tests/Python.EmbeddingTest.csproj | 2 +- src/embed_tests/{InitializeTest.cs => pyinitialize.cs} | 6 +----- 2 files changed, 2 insertions(+), 6 deletions(-) rename src/embed_tests/{InitializeTest.cs => pyinitialize.cs} (89%) diff --git a/src/embed_tests/Python.EmbeddingTest.csproj b/src/embed_tests/Python.EmbeddingTest.csproj index e1383f996..9cca794b4 100644 --- a/src/embed_tests/Python.EmbeddingTest.csproj +++ b/src/embed_tests/Python.EmbeddingTest.csproj @@ -75,7 +75,7 @@ - + diff --git a/src/embed_tests/InitializeTest.cs b/src/embed_tests/pyinitialize.cs similarity index 89% rename from src/embed_tests/InitializeTest.cs rename to src/embed_tests/pyinitialize.cs index 3c5974fb5..71aea58be 100644 --- a/src/embed_tests/InitializeTest.cs +++ b/src/embed_tests/pyinitialize.cs @@ -1,13 +1,9 @@ using NUnit.Framework; using Python.Runtime; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; namespace Python.EmbeddingTest { - public class InitializeTest + public class PyInitializeTest { [Test] public static void LoadSpecificArgs() From 368d4d175c55d79c896295a8aaec371520403d8e Mon Sep 17 00:00:00 2001 From: Victor Uriarte Date: Wed, 8 Feb 2017 12:35:52 -0700 Subject: [PATCH 4/4] Simplify embed_tests Since the previous bug involving initialize/finalize have been solved, we can confidently upgrade these test to use simpler format. Also Remove Assert fail messages NUnit already says the name of which test failed. --- src/embed_tests/pyimport.cs | 30 +++++++++++---------- src/embed_tests/pyiter.cs | 43 ++++++++++-------------------- src/embed_tests/pylong.cs | 27 +++++-------------- src/embed_tests/pyobject.cs | 25 ++++------------- src/embed_tests/pythonexception.cs | 7 +++++ 5 files changed, 48 insertions(+), 84 deletions(-) diff --git a/src/embed_tests/pyimport.cs b/src/embed_tests/pyimport.cs index dc9d90fe2..ebdc5b125 100644 --- a/src/embed_tests/pyimport.cs +++ b/src/embed_tests/pyimport.cs @@ -5,6 +5,18 @@ namespace Python.EmbeddingTest { + /// + /// Test Import unittests and regressions + /// + /// + /// Keeping in old-style SetUp/TearDown due to required SetUp. + /// The required directory structure was added to .\pythonnet\src\tests\ directory: + /// + PyImportTest/ + /// | - __init__.py + /// | + test/ + /// | | - __init__.py + /// | | - one.py + /// [TestFixture] public class PyImportTest { @@ -18,10 +30,8 @@ public void SetUp() /* Append the tests directory to sys.path * using reflection to circumvent the private - * modifiers placed on most Runtime methods. - */ - const string s = @"../../tests"; - + * modifiers placed on most Runtime methods. */ + const string s = "../../tests"; string testPath = Path.Combine(TestContext.CurrentContext.TestDirectory, s); IntPtr str = Runtime.Runtime.PyString_FromString(testPath); @@ -39,19 +49,11 @@ public void TearDown() /// /// Test subdirectory import /// - /// - /// The required directory structure was added to .\pythonnet\src\tests\ directory: - /// + PyImportTest/ - /// | - __init__.py - /// | + test/ - /// | | - __init__.py - /// | | - one.py - /// [Test] public void TestDottedName() { PyObject module = PythonEngine.ImportModule("PyImportTest.test.one"); - Assert.IsNotNull(module, ">>> import PyImportTest.test.one # FAILED"); + Assert.IsNotNull(module); } /// @@ -61,7 +63,7 @@ public void TestDottedName() public void TestSysArgsImportException() { PyObject module = PythonEngine.ImportModule("PyImportTest.sysargv"); - Assert.IsNotNull(module, ">>> import PyImportTest.sysargv # FAILED"); + Assert.IsNotNull(module); } } } diff --git a/src/embed_tests/pyiter.cs b/src/embed_tests/pyiter.cs index 1f0c651f8..b110903f6 100644 --- a/src/embed_tests/pyiter.cs +++ b/src/embed_tests/pyiter.cs @@ -1,45 +1,30 @@ -using System; using System.Collections.Generic; using NUnit.Framework; using Python.Runtime; namespace Python.EmbeddingTest { - [TestFixture] public class PyIterTest { - private IntPtr gs; - - [SetUp] - public void SetUp() - { - PythonEngine.Initialize(); - gs = PythonEngine.AcquireLock(); - } - - [TearDown] - public void TearDown() - { - PythonEngine.ReleaseLock(gs); - PythonEngine.Shutdown(); - } - [Test] public void TestOnPyList() { - var list = new PyList(); - list.Append(new PyString("foo")); - list.Append(new PyString("bar")); - list.Append(new PyString("baz")); - var result = new List(); - foreach (PyObject item in list) + using (Py.GIL()) { - result.Add(item.ToString()); + var list = new PyList(); + list.Append(new PyString("foo")); + list.Append(new PyString("bar")); + list.Append(new PyString("baz")); + var result = new List(); + foreach (PyObject item in list) + { + result.Add(item.ToString()); + } + Assert.AreEqual(3, result.Count); + Assert.AreEqual("foo", result[0]); + Assert.AreEqual("bar", result[1]); + Assert.AreEqual("baz", result[2]); } - Assert.AreEqual(3, result.Count); - Assert.AreEqual("foo", result[0]); - Assert.AreEqual("bar", result[1]); - Assert.AreEqual("baz", result[2]); } } } diff --git a/src/embed_tests/pylong.cs b/src/embed_tests/pylong.cs index 3a7365a61..37cf1042d 100644 --- a/src/embed_tests/pylong.cs +++ b/src/embed_tests/pylong.cs @@ -1,34 +1,19 @@ -using System; using NUnit.Framework; using Python.Runtime; namespace Python.EmbeddingTest { - [TestFixture] public class PyLongTest { - private IntPtr gs; - - [SetUp] - public void SetUp() - { - PythonEngine.Initialize(); - gs = PythonEngine.AcquireLock(); - } - - [TearDown] - public void TearDown() - { - PythonEngine.ReleaseLock(gs); - PythonEngine.Shutdown(); - } - [Test] public void TestToInt64() { - long largeNumber = 8L * 1024L * 1024L * 1024L; // 8 GB - var pyLargeNumber = new PyLong(largeNumber); - Assert.AreEqual(largeNumber, pyLargeNumber.ToInt64()); + using (Py.GIL()) + { + long largeNumber = 8L * 1024L * 1024L * 1024L; // 8 GB + var pyLargeNumber = new PyLong(largeNumber); + Assert.AreEqual(largeNumber, pyLargeNumber.ToInt64()); + } } } } diff --git a/src/embed_tests/pyobject.cs b/src/embed_tests/pyobject.cs index c0f18df39..f114d3d9e 100644 --- a/src/embed_tests/pyobject.cs +++ b/src/embed_tests/pyobject.cs @@ -1,33 +1,18 @@ -using System; using NUnit.Framework; using Python.Runtime; namespace Python.EmbeddingTest { - [TestFixture] public class PyObjectTest { - private IntPtr gs; - - [SetUp] - public void SetUp() - { - PythonEngine.Initialize(); - gs = PythonEngine.AcquireLock(); - } - - [TearDown] - public void TearDown() - { - PythonEngine.ReleaseLock(gs); - PythonEngine.Shutdown(); - } - [Test] public void TestUnicode() { - PyObject s = new PyString("foo\u00e9"); - Assert.AreEqual("foo\u00e9", s.ToString()); + using (Py.GIL()) + { + PyObject s = new PyString("foo\u00e9"); + Assert.AreEqual("foo\u00e9", s.ToString()); + } } } } diff --git a/src/embed_tests/pythonexception.cs b/src/embed_tests/pythonexception.cs index aaaf2c61c..0b7771d96 100644 --- a/src/embed_tests/pythonexception.cs +++ b/src/embed_tests/pythonexception.cs @@ -4,6 +4,13 @@ namespace Python.EmbeddingTest { + /// + /// Test Python Exceptions + /// + /// + /// Keeping this in the old-style SetUp/TearDown + /// to ensure that setup still works. + /// [TestFixture] public class PythonExceptionTest {