using OpenTap; using System; using System.Threading;
[Display("依赖资源测试")] publicclassDependencyResourceTest : TestStep { publicoverridevoidRun() { // 创建测试资源 var mainInstrument = new MockInstrument() { Name = "MainInstrument" }; var subInstrument = new MockInstrument() { Name = "SubInstrument" }; var powerSupply = new MockPowerSupply() { Name = "PowerSupply" }; // 设置依赖关系 mainInstrument.DependentResource = subInstrument; subInstrument.DependentResource = powerSupply; // 创建测试计划并执行 var plan = new TestPlan(); plan.ChildTestSteps.Add(this); // 验证资源开启顺序 Log.Info("开始执行测试计划,观察资源开启顺序..."); var result = plan.Execute(); Log.Info($"测试执行结果: {(result.Passed ? "通过" : "失败")}"); } }
// 创建测试计划执行示例 var testPlan = new TestPlan(); testPlan.Name = "Execution Demo";
// 添加测试步骤 var delayStep = new OpenTap.Tutorial.SimpleDelayTestStep(); delayStep.DelaySecs = 1.0; testPlan.Steps.Add(delayStep);
// 执行测试计划(带异常处理) try { var result = testPlan.Execute(); Console.WriteLine($"Test plan completed with verdict: {result.Verdict}"); Console.WriteLine($"Duration: {result.Duration.TotalSeconds} seconds"); } catch (OperationCanceledException) { Console.WriteLine("Test plan was cancelled"); } catch (Exception ex) { Console.WriteLine($"Test plan failed: {ex.Message}"); }
privatestaticICollection<TypeData> GetPlugins(PluginSearcher searcher, string baseTypeFullName) { TypeData baseType; if (!searcher.AllTypes.TryGetValue(baseTypeFullName, out baseType)) return Array.Empty<TypeData>();
var specializations = new List<TypeData>(); foreach (TypeData st in baseType.DerivedTypes) { if (st.TypeAttributes.HasFlag(TypeAttributes.Interface) || st.TypeAttributes.HasFlag(TypeAttributes.Abstract)) continue; if(shouldLoadAssembly(st.Assembly.Name, st.Assembly.Version)) specializations.Add(st); } return specializations; }
# 1. 创建测试目录 mkdir -p /tmp/opentap-plugin-test cd /tmp/opentap-plugin-test
# 2. 创建简单的测试插件 cat > TestPlugin.cs << 'EOF' using OpenTap; using System;
[Display("My Test Plugin")] public class TestPlugin : ITapPlugin { public string Name => "Test Plugin"; public string Description => "A simple test plugin"; } EOF