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
77
78
79
80
81
82
83
84
85
using System;
using System.Collections.Generic;
using System.IO;
namespace OpenTap.Engine.UnitTests;
 
static class Utilities
{
    public struct RunData
    {
        public TestPlanRun PlanRun;
        public TestStepRun[] StepRuns;
        public (Guid, ResultTable)[] Results;
        public (Guid runId, string artifactName, byte[] artifactData)[] Artifacts;
        public string Log;
    }
 
    class RunDataResultListener : ResultListener, IArtifactListener
    {
        List<TestStepRun> stepRuns = new();
        List<(Guid, ResultTable)> results = new();
        List<(Guid runId, string artifactName, byte[] artifactData)> artifacts = new();
        TestPlanRun planRun;
        string log;
        
        public override void OnTestStepRunCompleted(TestStepRun stepRun)
        {
            base.OnTestStepRunCompleted(stepRun);
            stepRuns.Add(stepRun);
        }
 
        public override void OnTestPlanRunCompleted(TestPlanRun planRun, Stream logStream)
        {
            base.OnTestPlanRunCompleted(planRun, logStream);
            this.planRun = planRun;
            log = new StreamReader(logStream).ReadToEnd();
        }
 
        public override void OnResultPublished(Guid stepRunId, ResultTable result)
        {
            base.OnResultPublished(stepRunId, result);
            results.Add((stepRunId, result));
        }
 
        public RunData GetData()
        {
            return new RunData
            {
                Artifacts = artifacts.ToArray(),
                Results = results.ToArray(),
                StepRuns = stepRuns.ToArray(),
                PlanRun = planRun,
                Log = log
            };
 
        }
        
        public void OnArtifactPublished(TestRun run, Stream artifactStream, string artifactName)
        {
            using (artifactStream)
            {
                artifacts.Add((run.Id, artifactName, artifactStream.GetBytes()));
            }
        }
    }
    
    public static RunData ExecuteReturnData(this TestPlan plan)
    {
        var runListener = new RunDataResultListener();
        plan.Execute(new IResultListener[]
        {
            runListener
        });
 
        return runListener.GetData();
 
    }
    
    public static byte[] GetBytes(this Stream stream)
    {
        using var memoryStream = new MemoryStream();
        stream.CopyTo(memoryStream);
        return memoryStream.ToArray();
 
    }
}