Repository navigation
Expand file tree
/
Copy pathCsvResultListener.py
More file actions
85 lines (71 loc) · 3.05 KB
/
Copy pathCsvResultListener.py
File metadata and controls
85 lines (71 loc) · 3.05 KB
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
"""
Example of how to make a Result Listener.
"""
import sys
import clr
import math
import opentap
from opentap import *
import os
import System.IO
from System.Text import StringBuilder
from System import String
import OpenTap
from OpenTap import Log, DisplayAttribute, Display, FilePathAttribute, FilePath
@attribute(Display("CSV ResultListener", "An example of a ResultListener.", "Python Example"))
class CsvPythonResultListener(PyResultListener):
# Use MacroString to get access to various macros for the file name.
# The <ResultName> macro is not supported in this example code as all the results
# are written to the same file.
FilePath = property(OpenTap.MacroString, None)\
.add_attribute(FilePath(FilePathAttribute.BehaviorChoice.Open, ".csv"))\
.add_attribute(Display("File Path", "File path for results."))
def __init__(self):
super(CsvPythonResultListener, self).__init__() # The base class initializer must be invoked.
self.sb = StringBuilder()
self.Name = "PyCSV"
self.FilePath = OpenTap.MacroString()
self.FilePath.Text = "<Date>/test.csv"
def OnTestPlanRunStart(self, planRun):
"""Called by TAP when the test plan starts."""
pass
def OnTestStepRunStart(self, stepRun):
"""Called by TAP when a test step starts."""
pass
def OnResultPublished(self, stepRun, result):
"""Called by TAP when a chunk of results are published."""
self.OnActivity()
for row in range(0, result.Rows):
first = True
for col in range(0, result.Columns.Length):
if first:
first = False
else:
self.sb.Append(", ")
self.sb.Append(str(result.Columns[col].Data.GetValue(row)))
self.sb.AppendLine("")
def OnTestPlanRunCompleted(self, planRun, logStream):
"""Called by TAP when the test plan completes."""
try:
# this code expands the macros used by FilePath.
fileName = self.FilePath.Expand(planRun)
# Create directory if not exists
directory = os.path.dirname(fileName)
if directory != "" and not os.path.exists(directory):
os.makedirs(directory)
# Then write to the file.
System.IO.File.WriteAllText(fileName, self.sb.ToString())
# Publish the csv file as an artifact. This feature is a available from OpenTAP 9.22.0
if hasattr(planRun, "PublishArtifact"):
planRun.PublishArtifact(fileName)
except Exception as e:
self.log.Debug(e)
def OnTestStepRunCompleted(self, stepRun):
"""Called by TAP when a test step completes."""
pass
def Open(self):
"""Called by TAP when the test plan starts."""
super(CsvPythonResultListener, self).Open()
def Close(self):
"""Called by TAP when the test plan ends."""
super(CsvPythonResultListener, self).Close()