Skip to content

Commit 6cc44d1

Browse files
committed
EmbeddedPythonTest uses importlib
1 parent eb3f030 commit 6cc44d1

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

src/embed_tests/TestPythonTests.cs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public void Dispose()
2727

2828
static IEnumerable<string[]> MyTestCases()
2929
{
30+
yield return new[] { "test_enum", "test_enum_standard_attrs" };
3031
yield return new[] { "test_generic", "test_missing_generic_type" };
3132
}
3233

@@ -43,11 +44,25 @@ public void EmbeddedPythonTest(string testFile, string testName)
4344
string path = Path.Combine(folder, testFile + ".py");
4445
if (!File.Exists(path)) throw new FileNotFoundException("Cannot find test file", path);
4546

47+
// importlib gives more helpful error messages than 'import'
48+
// https://docs.python.org/3/library/importlib.html#importing-a-source-file-directly
4649
PythonEngine.Exec($@"
4750
import sys
48-
sys.path.insert(0, r'{folder}')
49-
import {testFile}
50-
{testFile}.{testName}()
51+
import os
52+
fixtures_path = os.path.join(r'{folder}', 'fixtures')
53+
sys.path.append(fixtures_path)
54+
import clr
55+
clr.AddReference('Python.Test')
56+
sys.path.append(r'{folder}')
57+
import utils
58+
module_name = '{testFile}'
59+
file_path = r'{path}'
60+
import importlib.util
61+
spec = importlib.util.spec_from_file_location(module_name, file_path)
62+
module = importlib.util.module_from_spec(spec)
63+
sys.modules[module_name] = module
64+
spec.loader.exec_module(module)
65+
module.{testName}()
5166
");
5267
}
5368
}

0 commit comments

Comments
 (0)