- Notifications
You must be signed in to change notification settings - Fork749
Python tests can now be debugged by running them as embedded tests within NUnit#1341
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
4 commits Select commitHold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
6 changes: 5 additions & 1 deletion.github/workflows/main.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -49,9 +49,13 @@ jobs: | ||
- name: Python Tests | ||
run: pytest | ||
- name: Embedding tests | ||
lostmsu marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
run: dotnet test --runtime any-${{ matrix.platform }} src/embed_tests/ | ||
if: ${{ matrix.os != 'macos' }} # Not working right now, doesn't find libpython | ||
- name: Python tests run from .NET | ||
run: dotnet test --runtime any-${{ matrix.platform }} src/python_tests_runner/ | ||
if: ${{ matrix.os == 'windows' }} # Not working for others right now | ||
# TODO: Run perf tests | ||
# TODO: Run mono tests on Windows? |
1 change: 1 addition & 0 deletionsCHANGELOG.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletionspythonnet.sln
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletionssrc/python_tests_runner/Python.PythonTestsRunner.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFrameworks>net472;netcoreapp3.1</TargetFrameworks> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\runtime\Python.Runtime.csproj" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<PackageReference Include="NUnit" Version="3.*" /> | ||
<PackageReference Include="NUnit3TestAdapter" Version="3.*"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.*" /> | ||
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Condition="$(MSBuildRuntimeType) == 'Core'"> | ||
<Version>1.0.0</Version> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
</Project> |
82 changes: 82 additions & 0 deletionssrc/python_tests_runner/PythonTestRunner.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Text; | ||
using NUnit.Framework; | ||
using Python.Runtime; | ||
namespace Python.PythonTestsRunner | ||
{ | ||
public class PythonTestRunner | ||
{ | ||
[OneTimeSetUp] | ||
public void SetUp() | ||
{ | ||
PythonEngine.Initialize(); | ||
} | ||
[OneTimeTearDown] | ||
public void Dispose() | ||
{ | ||
PythonEngine.Shutdown(); | ||
} | ||
/// <summary> | ||
/// Selects the Python tests to be run as embedded tests. | ||
/// </summary> | ||
/// <returns></returns> | ||
static IEnumerable<string[]> PythonTestCases() | ||
{ | ||
// Add the test that you want to debug here. | ||
yield return new[] { "test_enum", "test_enum_standard_attrs" }; | ||
yield return new[] { "test_generic", "test_missing_generic_type" }; | ||
} | ||
tminka marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
/// <summary> | ||
/// Runs a test in src/tests/*.py as an embedded test. This facilitates debugging. | ||
/// </summary> | ||
/// <param name="testFile">The file name without extension</param> | ||
/// <param name="testName">The name of the test method</param> | ||
[TestCaseSource(nameof(PythonTestCases))] | ||
public void RunPythonTest(string testFile, string testName) | ||
{ | ||
// Find the tests directory | ||
string folder = typeof(PythonTestRunner).Assembly.Location; | ||
while (Path.GetFileName(folder) != "src") | ||
{ | ||
folder = Path.GetDirectoryName(folder); | ||
} | ||
folder = Path.Combine(folder, "tests"); | ||
string path = Path.Combine(folder, testFile + ".py"); | ||
if (!File.Exists(path)) throw new FileNotFoundException("Cannot find test file", path); | ||
// We could use 'import' below, but importlib gives more helpful error messages than 'import' | ||
// https://docs.python.org/3/library/importlib.html#importing-a-source-file-directly | ||
// Because the Python tests sometimes have relative imports, the module name must be inside the tests package | ||
PythonEngine.Exec($@" | ||
import sys | ||
import os | ||
sys.path.append(os.path.dirname(r'{folder}')) | ||
sys.path.append(os.path.join(r'{folder}', 'fixtures')) | ||
import clr | ||
clr.AddReference('Python.Test') | ||
import tests | ||
module_name = 'tests.{testFile}' | ||
file_path = r'{path}' | ||
import importlib.util | ||
spec = importlib.util.spec_from_file_location(module_name, file_path) | ||
module = importlib.util.module_from_spec(spec) | ||
sys.modules[module_name] = module | ||
try: | ||
spec.loader.exec_module(module) | ||
except ImportError as error: | ||
raise ImportError(str(error) + ' when sys.path=' + os.pathsep.join(sys.path)) | ||
module.{testName}() | ||
"); | ||
} | ||
} | ||
} |
4 changes: 3 additions & 1 deletionsrc/runtime/importhook.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.