using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Reflection;using System.Text;using NUnit.Framework;using Python.Runtime;using Python.Test;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_indexer", "test_boolean_indexer" };yield return new[] { "test_delegate", "test_bool_delegate" };yield return new[] { "test_subclass", "test_implement_interface_and_class" };}/// <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 directorystring 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 packagePythonEngine.Exec($@"import sysimport ossys.path.append(os.path.dirname(r'{folder}'))sys.path.append(os.path.join(r'{folder}', 'fixtures'))import clrclr.AddReference('Python.Test')import testsmodule_name = 'tests.{testFile}'file_path = r'{path}'import importlib.utilspec = importlib.util.spec_from_file_location(module_name, file_path)module = importlib.util.module_from_spec(spec)sys.modules[module_name] = moduletry:spec.loader.exec_module(module)except ImportError as error:raise ImportError(str(error) + ' when sys.path=' + os.pathsep.join(sys.path))module.{testName}()");}}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。