Skip to content

Commit 4919bff

Browse files
authored
Simplify Embedded tests & Add new tests (#369)
* Add Tuple tests * Update AppVeyor comments and show when tests start. * Rename InitializeTest to pyinitialize Keep consistent with other test classes. * 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.
1 parent 3995130 commit 4919bff

File tree

9 files changed

+148
-91
lines changed

9 files changed

+148
-91
lines changed

ci/appveyor_run_tests.ps1

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,22 @@ $RUNTIME_DIR = ".\src\runtime\bin\"
1212

1313
# Run python tests with C# coverage
1414
# why `2>&1 | %{ "$_" }`? see: http://stackoverflow.com/a/20950421/5208670
15+
Write-Host ("Starting Python tests") -ForegroundColor "Green"
1516
.$OPENCOVER -register:user -searchdirs:"$RUNTIME_DIR" -output:py.coverage -target:"$PY" -targetargs:src\tests\runtests.py -returntargetcode 2>&1 | %{ "$_" }
1617
$PYTHON_STATUS = $LastExitCode
1718
if ($PYTHON_STATUS -ne 0) {
1819
Write-Host "Python tests failed, continuing to embedded tests" -ForegroundColor "Red"
1920
}
2021

2122
# Run Embedded tests with C# coverage
23+
Write-Host ("Starting embedded tests") -ForegroundColor "Green"
2224
.$OPENCOVER -register:user -searchdirs:"$RUNTIME_DIR" -output:cs.coverage -target:"$NUNIT" -targetargs:"$CS_TESTS" -returntargetcode
2325
$NUNIT_STATUS = $LastExitCode
2426
if ($NUNIT_STATUS -ne 0) {
2527
Write-Host "Embedded tests failed" -ForegroundColor "Red"
2628
}
2729

28-
# Embedded tests failing due to open issues, pass/fail only on Python exit code
30+
# Set exit code to fail if either Python or Embedded tests failed
2931
if ($PYTHON_STATUS -ne 0 -or $NUNIT_STATUS -ne 0) {
3032
Write-Host "Tests failed" -ForegroundColor "Red"
3133
$host.SetShouldExit(1)

src/embed_tests/Python.EmbeddingTest.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,13 @@
7575
<None Include="packages.config" />
7676
</ItemGroup>
7777
<ItemGroup>
78-
<Compile Include="InitializeTest.cs" />
78+
<Compile Include="pyinitialize.cs" />
7979
<Compile Include="pyimport.cs" />
8080
<Compile Include="pyiter.cs" />
8181
<Compile Include="pylong.cs" />
8282
<Compile Include="pyobject.cs" />
8383
<Compile Include="pythonexception.cs" />
84+
<Compile Include="pytuple.cs" />
8485
</ItemGroup>
8586
<ItemGroup>
8687
<ProjectReference Include="..\runtime\Python.Runtime.csproj">

src/embed_tests/pyimport.cs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,18 @@
55

66
namespace Python.EmbeddingTest
77
{
8+
/// <summary>
9+
/// Test Import unittests and regressions
10+
/// </summary>
11+
/// <remarks>
12+
/// Keeping in old-style SetUp/TearDown due to required SetUp.
13+
/// The required directory structure was added to .\pythonnet\src\tests\ directory:
14+
/// + PyImportTest/
15+
/// | - __init__.py
16+
/// | + test/
17+
/// | | - __init__.py
18+
/// | | - one.py
19+
/// </remarks>
820
[TestFixture]
921
public class PyImportTest
1022
{
@@ -18,10 +30,8 @@ public void SetUp()
1830

1931
/* Append the tests directory to sys.path
2032
* using reflection to circumvent the private
21-
* modifiers placed on most Runtime methods.
22-
*/
23-
const string s = @"../../tests";
24-
33+
* modifiers placed on most Runtime methods. */
34+
const string s = "../../tests";
2535
string testPath = Path.Combine(TestContext.CurrentContext.TestDirectory, s);
2636

2737
IntPtr str = Runtime.Runtime.PyString_FromString(testPath);
@@ -39,19 +49,11 @@ public void TearDown()
3949
/// <summary>
4050
/// Test subdirectory import
4151
/// </summary>
42-
/// <remarks>
43-
/// The required directory structure was added to .\pythonnet\src\tests\ directory:
44-
/// + PyImportTest/
45-
/// | - __init__.py
46-
/// | + test/
47-
/// | | - __init__.py
48-
/// | | - one.py
49-
/// </remarks>
5052
[Test]
5153
public void TestDottedName()
5254
{
5355
PyObject module = PythonEngine.ImportModule("PyImportTest.test.one");
54-
Assert.IsNotNull(module, ">>> import PyImportTest.test.one # FAILED");
56+
Assert.IsNotNull(module);
5557
}
5658

5759
/// <summary>
@@ -61,7 +63,7 @@ public void TestDottedName()
6163
public void TestSysArgsImportException()
6264
{
6365
PyObject module = PythonEngine.ImportModule("PyImportTest.sysargv");
64-
Assert.IsNotNull(module, ">>> import PyImportTest.sysargv # FAILED");
66+
Assert.IsNotNull(module);
6567
}
6668
}
6769
}

src/embed_tests/InitializeTest.cs renamed to src/embed_tests/pyinitialize.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
using NUnit.Framework;
22
using Python.Runtime;
3-
using System;
4-
using System.Collections.Generic;
5-
using System.Linq;
6-
using System.Text;
73

84
namespace Python.EmbeddingTest
95
{
10-
public class InitializeTest
6+
public class PyInitializeTest
117
{
128
[Test]
139
public static void LoadSpecificArgs()

src/embed_tests/pyiter.cs

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,30 @@
1-
using System;
21
using System.Collections.Generic;
32
using NUnit.Framework;
43
using Python.Runtime;
54

65
namespace Python.EmbeddingTest
76
{
8-
[TestFixture]
97
public class PyIterTest
108
{
11-
private IntPtr gs;
12-
13-
[SetUp]
14-
public void SetUp()
15-
{
16-
PythonEngine.Initialize();
17-
gs = PythonEngine.AcquireLock();
18-
}
19-
20-
[TearDown]
21-
public void TearDown()
22-
{
23-
PythonEngine.ReleaseLock(gs);
24-
PythonEngine.Shutdown();
25-
}
26-
279
[Test]
2810
public void TestOnPyList()
2911
{
30-
var list = new PyList();
31-
list.Append(new PyString("foo"));
32-
list.Append(new PyString("bar"));
33-
list.Append(new PyString("baz"));
34-
var result = new List<string>();
35-
foreach (PyObject item in list)
12+
using (Py.GIL())
3613
{
37-
result.Add(item.ToString());
14+
var list = new PyList();
15+
list.Append(new PyString("foo"));
16+
list.Append(new PyString("bar"));
17+
list.Append(new PyString("baz"));
18+
var result = new List<string>();
19+
foreach (PyObject item in list)
20+
{
21+
result.Add(item.ToString());
22+
}
23+
Assert.AreEqual(3, result.Count);
24+
Assert.AreEqual("foo", result[0]);
25+
Assert.AreEqual("bar", result[1]);
26+
Assert.AreEqual("baz", result[2]);
3827
}
39-
Assert.AreEqual(3, result.Count);
40-
Assert.AreEqual("foo", result[0]);
41-
Assert.AreEqual("bar", result[1]);
42-
Assert.AreEqual("baz", result[2]);
4328
}
4429
}
4530
}

src/embed_tests/pylong.cs

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,19 @@
1-
using System;
21
using NUnit.Framework;
32
using Python.Runtime;
43

54
namespace Python.EmbeddingTest
65
{
7-
[TestFixture]
86
public class PyLongTest
97
{
10-
private IntPtr gs;
11-
12-
[SetUp]
13-
public void SetUp()
14-
{
15-
PythonEngine.Initialize();
16-
gs = PythonEngine.AcquireLock();
17-
}
18-
19-
[TearDown]
20-
public void TearDown()
21-
{
22-
PythonEngine.ReleaseLock(gs);
23-
PythonEngine.Shutdown();
24-
}
25-
268
[Test]
279
public void TestToInt64()
2810
{
29-
long largeNumber = 8L * 1024L * 1024L * 1024L; // 8 GB
30-
var pyLargeNumber = new PyLong(largeNumber);
31-
Assert.AreEqual(largeNumber, pyLargeNumber.ToInt64());
11+
using (Py.GIL())
12+
{
13+
long largeNumber = 8L * 1024L * 1024L * 1024L; // 8 GB
14+
var pyLargeNumber = new PyLong(largeNumber);
15+
Assert.AreEqual(largeNumber, pyLargeNumber.ToInt64());
16+
}
3217
}
3318
}
3419
}

src/embed_tests/pyobject.cs

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,18 @@
1-
using System;
21
using NUnit.Framework;
32
using Python.Runtime;
43

54
namespace Python.EmbeddingTest
65
{
7-
[TestFixture]
86
public class PyObjectTest
97
{
10-
private IntPtr gs;
11-
12-
[SetUp]
13-
public void SetUp()
14-
{
15-
PythonEngine.Initialize();
16-
gs = PythonEngine.AcquireLock();
17-
}
18-
19-
[TearDown]
20-
public void TearDown()
21-
{
22-
PythonEngine.ReleaseLock(gs);
23-
PythonEngine.Shutdown();
24-
}
25-
268
[Test]
279
public void TestUnicode()
2810
{
29-
PyObject s = new PyString("foo\u00e9");
30-
Assert.AreEqual("foo\u00e9", s.ToString());
11+
using (Py.GIL())
12+
{
13+
PyObject s = new PyString("foo\u00e9");
14+
Assert.AreEqual("foo\u00e9", s.ToString());
15+
}
3116
}
3217
}
3318
}

src/embed_tests/pythonexception.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44

55
namespace Python.EmbeddingTest
66
{
7+
/// <summary>
8+
/// Test Python Exceptions
9+
/// </summary>
10+
/// <remarks>
11+
/// Keeping this in the old-style SetUp/TearDown
12+
/// to ensure that setup still works.
13+
/// </remarks>
714
[TestFixture]
815
public class PythonExceptionTest
916
{

src/embed_tests/pytuple.cs

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
using NUnit.Framework;
2+
using Python.Runtime;
3+
4+
namespace Python.EmbeddingTest
5+
{
6+
public class PyTupleTest
7+
{
8+
[Test]
9+
public void TestPyTupleEmpty()
10+
{
11+
using (Py.GIL())
12+
{
13+
var t = new PyTuple();
14+
Assert.AreEqual(0, t.Length());
15+
}
16+
}
17+
18+
[Test]
19+
[ExpectedException("Python.Runtime.PythonException")]
20+
public void TestPyTupleInvalidAppend()
21+
{
22+
using (Py.GIL())
23+
{
24+
PyObject s = new PyString("foo");
25+
var t = new PyTuple();
26+
t.Concat(s);
27+
}
28+
}
29+
30+
[Test]
31+
public void TestPyTupleValidAppend()
32+
{
33+
using (Py.GIL())
34+
{
35+
var t0 = new PyTuple();
36+
var t = new PyTuple();
37+
t.Concat(t0);
38+
Assert.IsNotNull(t);
39+
Assert.IsInstanceOf(typeof(PyTuple), t);
40+
}
41+
}
42+
43+
[Test]
44+
public void TestPyTupleIsTupleType()
45+
{
46+
using (Py.GIL())
47+
{
48+
var s = new PyString("foo");
49+
var t = new PyTuple();
50+
Assert.IsTrue(PyTuple.IsTupleType(t));
51+
Assert.IsFalse(PyTuple.IsTupleType(s));
52+
}
53+
}
54+
55+
[Test]
56+
public void TestPyTupleStringConvert()
57+
{
58+
using (Py.GIL())
59+
{
60+
PyObject s = new PyString("foo");
61+
PyTuple t = PyTuple.AsTuple(s);
62+
Assert.IsNotNull(t);
63+
Assert.IsInstanceOf(typeof(PyTuple), t);
64+
Assert.AreEqual("f", t[0].ToString());
65+
Assert.AreEqual("o", t[1].ToString());
66+
Assert.AreEqual("o", t[2].ToString());
67+
}
68+
}
69+
70+
[Test]
71+
public void TestPyTupleValidConvert()
72+
{
73+
using (Py.GIL())
74+
{
75+
var l = new PyList();
76+
PyTuple t = PyTuple.AsTuple(l);
77+
Assert.IsNotNull(t);
78+
Assert.IsInstanceOf(typeof(PyTuple), t);
79+
}
80+
}
81+
82+
[Test]
83+
public void TestNewPyTupleFromPyTuple()
84+
{
85+
using (Py.GIL())
86+
{
87+
var t0 = new PyTuple();
88+
var t = new PyTuple(t0);
89+
Assert.IsNotNull(t);
90+
Assert.IsInstanceOf(typeof(PyTuple), t);
91+
}
92+
}
93+
}
94+
}

0 commit comments

Comments
 (0)