chr
2026-04-05 fe750b791d5b517cc4e9bc8e99a9a75139a0cfba
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//            Copyright Keysight Technologies 2012-2019
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, you can obtain one at http://mozilla.org/MPL/2.0/.
using NUnit.Framework;
using System;
using System.Linq;
using System.Threading;
using System.Collections.Generic;
using OpenTap.Engine.UnitTests;
 
namespace OpenTap.EngineUnitTestUtils
{
    /// <summary>
    ///This is a base class for test classes intended to run a TestStep as a Unit Test
    ///</summary>
    [TestFixture]
    public abstract class TestStepTestBase
    {
        private List<String> allowedLogErrors = new List<string>();
 
        private TestContext testContextInstance;
        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get { return testContextInstance; }
            set { testContextInstance = value; }
        }
 
        protected void RunTestStep(ITestStep step, List<string> allowedLogErrors)
        {
            this.allowedLogErrors = allowedLogErrors;
            RunTestStep(step);
        }
 
        /// <summary>
        /// 
        /// </summary>
        /// <param name="step"></param>
        protected void RunTestStep(ITestStep step)
        {
            TestTraceListener trace = new TestTraceListener();
            PlanRunCollectorListener pl = new PlanRunCollectorListener();
            Log.AddListener(trace);
 
            TestPlan plan = new TestPlan();
            SetupPlan(plan, step);
            TestPlanRun run;
            using (Mutex mux = new Mutex(false, "TAPDatabaseAccessMutex"))
            {
                mux.WaitOne();
 
                run = plan.Execute(ResultSettings.Current.Concat(new IResultListener[] { pl }));
                mux.ReleaseMutex();
            }
            Log.RemoveListener(trace);
            CheckForInconclusive(trace.ErrorMessage);
            trace.AssertErrors(allowedLogErrors);
            Assert.AreEqual(Verdict.Pass, pl.StepRuns.First().Verdict, "Step ran to completion but verdict was 'fail'.\r\nLog:\r\n" + trace.allLog.ToString());
        }
 
        protected virtual void CheckForInconclusive(List<string> errorMessage)
        {
            if (errorMessage.Any(em => em.Contains("Failed to open resource")))
                Assert.Inconclusive(string.Join(Environment.NewLine, errorMessage));
        }
 
        protected virtual void SetupPlan(TestPlan plan, ITestStep step)
        {
            plan.Steps.Add(step);
        }
    }
}