🌐 AI搜索 & 代理 主页
Skip to content

Commit a38eddc

Browse files
committed
Use the actual pytest runner
1 parent bd33b3f commit a38eddc

File tree

5 files changed

+29
-33
lines changed

5 files changed

+29
-33
lines changed

.github/workflows/main.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,8 @@ jobs:
9292
run: pytest --runtime netfx
9393

9494
- name: Python tests run from .NET
95+
# For some reason, it won't find pytest on the Windows + 3.10
96+
# combination, which hints that it does not handle the venv properly in
97+
# this combination.
98+
if: ${{ matrix.os.category != 'windows' || matrix.python != '3.10' }}
9599
run: dotnet test --runtime any-${{ matrix.os.platform }} src/python_tests_runner/

Directory.Build.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<FullVersion>$([System.IO.File]::ReadAllText("$(MSBuildThisFileDirectory)version.txt").Trim())</FullVersion>
1010
<VersionPrefix>$(FullVersion.Split('-', 2)[0])</VersionPrefix>
1111
<VersionSuffix Condition="$(FullVersion.Contains('-'))">$(FullVersion.Split('-', 2)[1])</VersionSuffix>
12+
<RepositoryRoot>$(MSBuildThisFileDirectory)</RepositoryRoot>
1213
</PropertyGroup>
1314
<ItemGroup>
1415
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />

src/python_tests_runner/Python.PythonTestsRunner.csproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,12 @@
2323
</PackageReference>
2424
</ItemGroup>
2525

26+
<Target Name="WriteRepoRootFile" AfterTargets="Build">
27+
<WriteLinesToFile
28+
File="$(OutputPath)tests_location.txt"
29+
Lines="$(RepositoryRoot)/tests"
30+
Overwrite="true"
31+
/>
32+
</Target>
33+
2634
</Project>

src/python_tests_runner/PythonTestRunner.cs

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,29 @@
88
using NUnit.Framework;
99

1010
using Python.Runtime;
11-
using Python.Test;
1211

1312
namespace Python.PythonTestsRunner
1413
{
1514
public class PythonTestRunner
1615
{
16+
string OriginalDirectory;
17+
1718
[OneTimeSetUp]
1819
public void SetUp()
1920
{
2021
PythonEngine.Initialize();
22+
OriginalDirectory = Environment.CurrentDirectory;
23+
24+
var codeDir = File.ReadAllText("tests_location.txt").Trim();
25+
TestContext.Progress.WriteLine($"Changing working directory to {codeDir}");
26+
Environment.CurrentDirectory = codeDir;
2127
}
2228

2329
[OneTimeTearDown]
2430
public void Dispose()
2531
{
2632
PythonEngine.Shutdown();
33+
Environment.CurrentDirectory = OriginalDirectory;
2734
}
2835

2936
/// <summary>
@@ -46,39 +53,15 @@ static IEnumerable<string[]> PythonTestCases()
4653
[TestCaseSource(nameof(PythonTestCases))]
4754
public void RunPythonTest(string testFile, string testName)
4855
{
49-
// Find the tests directory
50-
string folder = typeof(PythonTestRunner).Assembly.Location;
51-
while (Path.GetFileName(folder) != "src")
56+
using dynamic pytest = Py.Import("pytest");
57+
58+
using var args = new PyList();
59+
args.Append(new PyString($"{testFile}.py::{testName}"));
60+
int res = pytest.main(args);
61+
if (res != 0)
5262
{
53-
folder = Path.GetDirectoryName(folder);
63+
Assert.Fail($"Python test {testFile}.{testName} failed");
5464
}
55-
folder = Path.Combine(folder, "..", "tests");
56-
string path = Path.Combine(folder, testFile + ".py");
57-
if (!File.Exists(path)) throw new FileNotFoundException("Cannot find test file", path);
58-
59-
// We could use 'import' below, but importlib gives more helpful error messages than 'import'
60-
// https://docs.python.org/3/library/importlib.html#importing-a-source-file-directly
61-
// Because the Python tests sometimes have relative imports, the module name must be inside the tests package
62-
PythonEngine.Exec($@"
63-
import sys
64-
import os
65-
sys.path.append(os.path.dirname(r'{folder}'))
66-
sys.path.append(os.path.join(r'{folder}', 'fixtures'))
67-
import clr
68-
clr.AddReference('Python.Test')
69-
import tests
70-
module_name = 'tests.{testFile}'
71-
file_path = r'{path}'
72-
import importlib.util
73-
spec = importlib.util.spec_from_file_location(module_name, file_path)
74-
module = importlib.util.module_from_spec(spec)
75-
sys.modules[module_name] = module
76-
try:
77-
spec.loader.exec_module(module)
78-
except ImportError as error:
79-
raise ImportError(str(error) + ' when sys.path=' + os.pathsep.join(sys.path))
80-
module.{testName}()
81-
");
8265
}
8366
}
8467
}

src/runtime/Util/PythonEnvironment.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ private static Dictionary<string, string> TryParse(string venvCfg)
161161

162162
private static string ProgramNameFromPath(string path)
163163
{
164-
if (Runtime.IsWindows)
164+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
165165
{
166166
return Path.Combine(path, "Scripts", "python.exe");
167167
}

0 commit comments

Comments
 (0)