-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathConsoleOutputTests.cs
More file actions
213 lines (170 loc) · 8.15 KB
/
ConsoleOutputTests.cs
File metadata and controls
213 lines (170 loc) · 8.15 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
using System;
using System.IO;
using Microsoft.PowerApps.TestEngine.System;
using Xunit;
namespace Microsoft.PowerApps.TestEngine.Tests
{
public class ConsoleOutputTests
{
TestEngineEventHandler _testEngineEventHandler;
public ConsoleOutputTests()
{
_testEngineEventHandler = new TestEngineEventHandler();
}
[Fact]
public void TestEncounteredException()
{
// Specify expected result and output object
var expected = " Message";
var printer = new StringWriter();
Exception ex = new Exception("Message");
// Set output
Console.SetOut(printer);
// Run function
_testEngineEventHandler.EncounteredException(ex);
// Assert that the expected output matches the console output of the function
Assert.Contains(expected, printer.ToString());
}
[Fact]
public void TestSuiteBegin()
{
var printer = new StringWriter();
// Set output
Console.SetOut(printer);
// Run function
_testEngineEventHandler.SuiteBegin("TestSuiteName", "./testDirectory", "Chromium", "make.powerapps.com/testapp&source=testengine");
// Assert that the expected output matches the console output of the function
Assert.Contains("Running test suite: TestSuiteName", printer.ToString());
Assert.Contains("\n Test results will be stored in: ./testDirectory", printer.ToString());
Assert.Contains("\n Browser: Chromium", printer.ToString());
Assert.Contains("\n App URL: make.powerapps.com/testapp&source=testengine", printer.ToString());
}
[Fact]
public void TestSuiteEnd()
{
var printer = new StringWriter();
// Set output
Console.SetOut(printer);
_testEngineEventHandler.CasesTotal = 11;
_testEngineEventHandler.CasesPassed = 6;
// Run function
_testEngineEventHandler.SuiteEnd();
// Assert that the expected output matches the console output of the function
Assert.Contains("\nTest suite summary", printer.ToString());
Assert.Contains("Total cases: 11", printer.ToString());
Assert.Contains("Cases passed: 6", printer.ToString());
Assert.Contains("Cases failed: 5", printer.ToString());
}
[Fact]
public void TestTestCaseBegin()
{
// Specify expected result and output object
var expected = "Test case: MyCase";
var printer = new StringWriter();
// Set output
Console.SetOut(printer);
// Run function
_testEngineEventHandler.TestCaseBegin("MyCase");
// Assert that the expected output matches the console output of the function
Assert.Contains(expected, printer.ToString());
}
[Fact]
public void TestTestCaseEndPassed()
{
// Specify expected result and output object
var expected = " Result: Passed";
var printer = new StringWriter();
// Set output
Console.SetOut(printer);
// Run function
_testEngineEventHandler.TestCaseEnd(true);
// Assert that the expected output matches the console output of the function
Assert.Contains(expected, printer.ToString());
}
[Fact]
public void TestTestCaseEndFailed()
{
// Specify expected result and output object
var expected = " Result: Failed";
var printer = new StringWriter();
// Set output
Console.SetOut(printer);
// Run function
_testEngineEventHandler.TestCaseEnd(false);
// Assert that the expected output matches the console output of the function
Assert.Contains(expected, printer.ToString());
}
[Fact]
public void TestMultipleBrowserRuns()
{
var printer = new StringWriter();
// Set output
Console.SetOut(printer);
// Run tests on first Browser
_testEngineEventHandler.SetAndInitializeCounters(2);
_testEngineEventHandler.SuiteBegin("TestSuiteName", "./testDirectory", "Chromium", "make.powerapps.com/testapp&source=testengine");
_testEngineEventHandler.TestCaseBegin("Case1");
_testEngineEventHandler.TestCaseEnd(true);
_testEngineEventHandler.TestCaseBegin("Case2");
_testEngineEventHandler.TestCaseEnd(false);
_testEngineEventHandler.SuiteEnd();
// Run tests on second Browser
_testEngineEventHandler.SetAndInitializeCounters(2);
_testEngineEventHandler.SuiteBegin("TestSuiteName", "./testDirectory", "Firefox", "make.powerapps.com/testapp&source=testengine");
_testEngineEventHandler.TestCaseBegin("Case1");
_testEngineEventHandler.TestCaseEnd(true);
_testEngineEventHandler.TestCaseBegin("Case2");
_testEngineEventHandler.TestCaseEnd(false);
_testEngineEventHandler.SuiteEnd();
// Assert that the expected console output matches
Assert.Contains("\nTest suite summary", printer.ToString());
Assert.Contains("Total cases: 2", printer.ToString());
Assert.Contains("Cases passed: 1", printer.ToString());
Assert.Contains("Cases failed: 1", printer.ToString());
// Assert none of the browser runs failed to show incorrect failed cases
Assert.DoesNotContain("Cases failed: 0", printer.ToString());
}
[Fact]
public void TestEncounteredUserAppException()
{
// Specify expected result and output object
var expected = TestEngineEventHandler.UserAppExceptionMessage;
var printer = new StringWriter();
Exception ex = new UserAppException();
// Set output
Console.SetOut(printer);
// Run function
_testEngineEventHandler.EncounteredException(ex);
// Assert that the expected output matches the console output
Assert.Contains(expected, printer.ToString());
}
[Theory]
[ClassData(typeof(UserInputExceptionDataGenerator))]
public void TestEncounteredUserInputException(string exceptionName, string expectedMessage)
{
// Specify expected result and output object
var printer = new StringWriter();
Exception ex = new UserInputException(exceptionName);
// Set output
Console.SetOut(printer);
// Run function
_testEngineEventHandler.EncounteredException(ex);
// Assert that the expected output matches the console output
Assert.Contains(expectedMessage, printer.ToString());
}
class UserInputExceptionDataGenerator : TheoryData<string, string>
{
public UserInputExceptionDataGenerator()
{
Add(nameof(UserInputException.ErrorMapping.UserInputExceptionInvalidTestSettings), TestEngineEventHandler.UserInputExceptionInvalidTestSettingsMessage);
Add(nameof(UserInputException.ErrorMapping.UserInputExceptionInvalidOutputPath), TestEngineEventHandler.UserInputExceptionInvalidOutputPathMessage);
Add(nameof(UserInputException.ErrorMapping.UserInputExceptionInvalidFilePath), TestEngineEventHandler.UserInputExceptionInvalidFilePathMessage);
Add(nameof(UserInputException.ErrorMapping.UserInputExceptionLoginCredential), TestEngineEventHandler.UserInputExceptionLoginCredentialMessage);
Add(nameof(UserInputException.ErrorMapping.UserInputExceptionTestConfig), TestEngineEventHandler.UserInputExceptionTestConfigMessage);
Add(nameof(UserInputException.ErrorMapping.UserInputExceptionYAMLFormat), TestEngineEventHandler.UserInputExceptionYAMLFormatMessage);
}
}
}
}