Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita38eddc

Browse files
committed
Use the actual pytest runner
1 parentbd33b3f commita38eddc

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
<VersionSuffixCondition="$(FullVersion.Contains('-'))">$(FullVersion.Split('-', 2)[1])</VersionSuffix>
12+
<RepositoryRoot>$(MSBuildThisFileDirectory)</RepositoryRoot>
1213
</PropertyGroup>
1314
<ItemGroup>
1415
<PackageReferenceInclude="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+
<TargetName="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
usingNUnit.Framework;
99

1010
usingPython.Runtime;
11-
usingPython.Test;
1211

1312
namespacePython.PythonTestsRunner
1413
{
1514
publicclassPythonTestRunner
1615
{
16+
stringOriginalDirectory;
17+
1718
[OneTimeSetUp]
1819
publicvoidSetUp()
1920
{
2021
PythonEngine.Initialize();
22+
OriginalDirectory=Environment.CurrentDirectory;
23+
24+
varcodeDir=File.ReadAllText("tests_location.txt").Trim();
25+
TestContext.Progress.WriteLine($"Changing working directory to{codeDir}");
26+
Environment.CurrentDirectory=codeDir;
2127
}
2228

2329
[OneTimeTearDown]
2430
publicvoidDispose()
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
publicvoidRunPythonTest(stringtestFile,stringtestName)
4855
{
49-
// Find the tests directory
50-
stringfolder=typeof(PythonTestRunner).Assembly.Location;
51-
while(Path.GetFileName(folder)!="src")
56+
usingdynamicpytest=Py.Import("pytest");
57+
58+
usingvarargs=newPyList();
59+
args.Append(newPyString($"{testFile}.py::{testName}"));
60+
intres=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-
stringpath=Path.Combine(folder,testFile+".py");
57-
if(!File.Exists(path))thrownewFileNotFoundException("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
privatestaticstringProgramNameFromPath(stringpath)
163163
{
164-
if(Runtime.IsWindows)
164+
if(RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
165165
{
166166
returnPath.Combine(path,"Scripts","python.exe");
167167
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp