/
TestConverterTests.cs
332 lines (278 loc) · 15.1 KB
/
TestConverterTests.cs
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
// ***********************************************************************
// Copyright (c) 2011-2021 Charlie Poole, Terje Sandstrom
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ***********************************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using NSubstitute;
using NUnit.Framework;
using NUnit.VisualStudio.TestAdapter.Dump;
using NUnit.VisualStudio.TestAdapter.NUnitEngine;
namespace NUnit.VisualStudio.TestAdapter.Tests
{
using Fakes;
[Category("TestConverter")]
public class TestConverterTests
{
private NUnitDiscoveryTestCase fakeTestNode;
private TestConverter testConverter;
[SetUp]
public void SetUp()
{
var xDoc = XDocument.Parse(FakeTestData.TestXml);
var parent = Substitute.For<INUnitDiscoveryCanHaveTestFixture>();
parent.Parent.Returns(null as INUnitDiscoverySuiteBase);
var className = xDoc.Root.Attribute("classname").Value;
var tf = DiscoveryConverter.ExtractTestFixture(parent, xDoc.Root, className);
var tcs = DiscoveryConverter.ExtractTestCases(tf, xDoc.Root);
Assert.That(tcs.Count(), Is.EqualTo(1), "Setup: More than one test case in fake data");
fakeTestNode = tcs.Single();
var settings = Substitute.For<IAdapterSettings>();
settings.ConsoleOut.Returns(0);
settings.UseTestNameInConsoleOutput.Returns(false);
settings.CollectSourceInformation.Returns(true);
var discoveryConverter = Substitute.For<IDiscoveryConverter>();
testConverter = new TestConverter(new TestLogger(new MessageLoggerStub()), FakeTestData.AssemblyPath, settings, discoveryConverter);
}
[TearDown]
public void TearDown()
{
testConverter?.Dispose();
}
[Test]
public void CanMakeTestCaseFromTest()
{
var testCase = testConverter.ConvertTestCase(fakeTestNode);
CheckTestCase(testCase);
}
[Test]
public void CanMakeTestCaseFromTestWithCache()
{
var testCase = testConverter.ConvertTestCase(fakeTestNode);
CheckTestCase(testCase);
Assert.That(testConverter.TraitsCache.Keys.Count, Is.EqualTo(1));
Assert.That(testConverter.TraitsCache["121"].Traits.Count, Is.EqualTo(1));
var parentTrait = testConverter.TraitsCache["121"].Traits;
Assert.That(parentTrait[0].Name, Is.EqualTo("Category"));
Assert.That(parentTrait[0].Value, Is.EqualTo("super"));
}
[Ignore("To do")]
[Test]
public void CanMakeTestCaseShouldBuildTraitsCache()
{
var xmlNodeList = FakeTestData.GetTestNodes();
var tf = Substitute.For<INUnitDiscoveryCanHaveTestCases>();
foreach (XmlNode node in xmlNodeList)
{
var xElem = XElement.Load(node.CreateNavigator().ReadSubtree());
var tc = DiscoveryConverter.ExtractTestCase(tf, xElem);
var testCase = testConverter.ConvertTestCase(tc);
}
var traitsCache = testConverter.TraitsCache;
Assert.Multiple(() =>
{
// There are 12 ids in the TestXml2, but will be storing only ancestor properties.
// Not the leaf node test-case properties.
Assert.That(traitsCache.Keys.Count, Is.EqualTo(7));
// Even though ancestor doesn't have any properties. Will be storing their ids.
// So that we will not make call SelectNodes call again.
CheckNodesWithNoProperties(traitsCache);
// Will not be storing leaf nodes test-case nodes in the cache.
CheckNoTestCaseNodesExist(traitsCache);
// Checking assembly level attribute.
CheckNodeProperties(traitsCache, "0-1009",
new[] { new KeyValuePair<string, string>("Category", "AsmCat") });
// Checking Class level attributes base class & dervied class
CheckNodeProperties(traitsCache, "0-1000",
new[] { new KeyValuePair<string, string>("Category", "BaseClass") });
CheckNodeProperties(traitsCache, "0-1002",
new[]
{
new KeyValuePair<string, string>("Category", "DerivedClass"),
new KeyValuePair<string, string>("Category", "BaseClass")
});
// Checking Nested class attributes.
CheckNodeProperties(traitsCache, "0-1005", new[] { new KeyValuePair<string, string>("Category", "NS1") });
CheckNodeProperties(traitsCache, "0-1007", new[] { new KeyValuePair<string, string>("Category", "NS2") });
});
}
[Test]
public void ConvertedTestCaseIsCached()
{
testConverter.ConvertTestCase(fakeTestNode);
var testCase = testConverter.GetCachedTestCase("123");
CheckTestCase(testCase);
}
[Test]
public void CannotMakeTestResultWhenTestCaseIsNotInCache()
{
var fakeResultNode = new NUnitTestEventTestCase(FakeTestData.GetResultNode());
var results = testConverter.GetVsTestResults(fakeResultNode, Enumerable.Empty<INUnitTestEventTestOutput>().ToList());
Assert.That(results.TestResults.Count, Is.EqualTo(0));
}
[Test]
public void CanMakeTestResultFromNUnitTestResult()
{
// This should put the TestCase in the cache
var cachedTestCase = testConverter.ConvertTestCase(fakeTestNode);
var fakeResultNode = new NUnitTestEventTestCase(FakeTestData.GetResultNode());
var testResults = testConverter.GetVsTestResults(fakeResultNode, Enumerable.Empty<INUnitTestEventTestOutput>().ToList());
var testResult = testResults.TestResults[0];
var testCase = testResult.TestCase;
Assert.That(testCase, Is.SameAs(cachedTestCase));
CheckTestCase(testCase);
Assert.That(testResult.Outcome, Is.EqualTo(TestOutcome.Passed));
Assert.That(testResult.ErrorMessage, Is.EqualTo(null));
Assert.That(testResult.Duration, Is.EqualTo(TimeSpan.FromSeconds(1.234)));
}
[TestCase(
"<test-output stream=\"Error\" testid=\"0-1001\" testname=\"UnitTests.Test1\"><![CDATA[some stdErr]]></test-output>",
"StdErrMsgs:some stdErr")]
[TestCase(
"<test-output stream=\"Progress\" testid=\"0-1001\" testname=\"UnitTests.Test1\"><![CDATA[some text]]></test-output>",
"")]
[TestCase(
"<test-output stream=\"Error\" testid=\"0-1001\" testname=\"UnitTests.Test1\"><![CDATA[some stdErr]]></test-output>"
+ ";<test-output stream=\"Progress\" testid=\"0-1001\" testname=\"UnitTests.Test1\"><![CDATA[some text]]></test-output>",
"StdErrMsgs:some stdErr")]
public void CanMakeTestResultFromNUnitTestResult2(string output, string expectedMessages)
{
var cachedTestCase = testConverter.ConvertTestCase(fakeTestNode);
var fakeResultNode = new NUnitTestEventTestCase(FakeTestData.GetResultNode());
var outputNodes = output.Split(';').Select(i => new NUnitTestEventTestOutput(XmlHelper.CreateXmlNode(i.Trim()))).ToList();
var outputNodesCollection = new List<INUnitTestEventTestOutput>(outputNodes);
var testResults = testConverter.GetVsTestResults(fakeResultNode, outputNodesCollection);
var testResult = testResults.TestResults[0];
var actualMessages = string.Join(";", testResult.Messages.Select(i => i.Category + ":" + i.Text));
Assert.That(actualMessages, Is.EqualTo(expectedMessages));
}
#region Attachment tests
[Test]
public void Attachments_CorrectAmountOfConvertedAttachments()
{
var cachedTestCase = testConverter.ConvertTestCase(fakeTestNode);
var fakeResultNode = new NUnitTestEventTestCase(FakeTestData.GetResultNode());
var testResults = testConverter.GetVsTestResults(fakeResultNode, Enumerable.Empty<INUnitTestEventTestOutput>().ToList());
var fakeAttachments = fakeResultNode.NUnitAttachments
.Where(n => !string.IsNullOrEmpty(n.FilePath))
.ToArray();
var convertedAttachments = testResults.TestResults
.SelectMany(tr => tr.Attachments.SelectMany(ats => ats.Attachments))
.ToArray();
Assert.Multiple(() =>
{
Assert.That(convertedAttachments.Length, Is.GreaterThan(0), "Some converted attachments were expected");
Assert.That(convertedAttachments.Length, Is.EqualTo(fakeAttachments.Length), "Attachments are not converted");
});
}
[Test]
public void Attachments_AllFilePathesStartWithFileScheme()
{
const string fileUriScheme = "file://";
const string errorMessage = "Path must start with file:// uri scheme";
var cachedTestCase = testConverter.ConvertTestCase(fakeTestNode);
var fakeResultNode = new NUnitTestEventTestCase(FakeTestData.GetResultNode().AsString());
var testResults = testConverter.GetVsTestResults(fakeResultNode, Enumerable.Empty<INUnitTestEventTestOutput>().ToList());
var convertedAttachments = testResults.TestResults
.SelectMany(tr => tr.Attachments.SelectMany(ats => ats.Attachments))
.ToArray();
foreach (var attachment in convertedAttachments)
{
var originalPath = attachment.Uri.OriginalString;
Assert.That(originalPath.LastIndexOf(fileUriScheme), Is.EqualTo(0), errorMessage);
}
}
#endregion Attachment tests
private void CheckTestCase(TestCase testCase)
{
Assert.That(testCase.FullyQualifiedName, Is.EqualTo(FakeTestData.FullyQualifiedName));
Assert.That(testCase.DisplayName, Is.EqualTo(FakeTestData.DisplayName));
Assert.That(testCase.Source, Is.SamePath(FakeTestData.AssemblyPath));
if (testCase.CodeFilePath != null) // Unavailable if not running under VS
{
Assert.That(testCase.CodeFilePath, Is.SamePath(FakeTestData.CodeFile));
Assert.That(testCase.LineNumber, Is.EqualTo(FakeTestData.LineNumber));
}
var traitList = testCase.GetTraits().Select(trait => trait.Name + ":" + trait.Value).ToList();
Assert.That(traitList, Is.EquivalentTo(new[] { "Priority:medium" }));
Assert.That(testCase.GetCategories(), Is.EquivalentTo(new[] { "super", "cat1", }));
}
private void CheckNodesWithNoProperties(IDictionary<string, TraitsFeature.CachedTestCaseInfo> cache)
{
Assert.That(cache["2"].Traits.Count, Is.EqualTo(0));
Assert.That(cache["0-1010"].Traits.Count, Is.EqualTo(0));
}
private void CheckNoTestCaseNodesExist(IDictionary<string, TraitsFeature.CachedTestCaseInfo> cache)
{
Assert.That(!cache.ContainsKey("0-1008"));
Assert.That(!cache.ContainsKey("0-1006"));
Assert.That(!cache.ContainsKey("0-1004"));
Assert.That(!cache.ContainsKey("0-1003"));
Assert.That(!cache.ContainsKey("0-1001"));
}
private void CheckNodeProperties(IDictionary<string, TraitsFeature.CachedTestCaseInfo> cache, string id, KeyValuePair<string, string>[] kps)
{
Assert.That(cache.ContainsKey(id));
Assert.That(cache[id].Traits.Count, Is.EqualTo(kps.Count()));
var info = cache[id];
foreach (var kp in kps)
{
Assert.That(info.Traits.Any(t => t.Name == kp.Key && t.Value == kp.Value));
}
}
[Description("Third-party runners may opt to depend on this. https://github.com/nunit/nunit3-vs-adapter/issues/487#issuecomment-389222879")]
[TestCase("NonExplicitParent.ExplicitTest")]
[TestCase("ExplicitParent.NonExplicitTest")]
public static void NUnitExplicitBoolPropertyIsProvidedForThirdPartyRunnersInExplicitTestCases(string testName)
{
var testCase = GetSampleTestCase(testName);
var property = testCase.Properties.Single(p =>
p.Id == "NUnit.Explicit"
&& p.GetValueType() == typeof(bool));
Assert.That(testCase.GetPropertyValue(property), Is.True);
}
[TestCase("NonExplicitParent.NonExplicitTestWithExplicitCategory")]
public static void NUnitExplicitBoolPropertyIsNotProvidedForThirdPartyRunnersInNonExplicitTestCases(string testName)
{
var testCase = GetSampleTestCase(testName);
Assert.That(testCase, Has.Property("Properties").With.None.With.Property("Id").EqualTo("NUnit.Explicit"));
}
private static TestCase GetSampleTestCase(string fullyQualifiedName)
{
return TestCaseUtils.ConvertTestCases(@"
<test-suite id='1' name='NonExplicitParent' fullname='NonExplicitParent'>
<test-case id='2' name='NonExplicitTest' fullname='NonExplicitParent.NonExplicitTestWithExplicitCategory'>
<properties>
<property name='Category' value='Explicit' />
</properties>
</test-case>
<test-case id='3' name='ExplicitTest' fullname='NonExplicitParent.ExplicitTest' runstate='Explicit' />
</test-suite>
<test-suite id='4' name='ExplicitParent' fullname='ExplicitParent' runstate='Explicit'>
<test-case id='5' name='NonExplicitTest' fullname='ExplicitParent.NonExplicitTest' />
</test-suite>").Single(t => t.FullyQualifiedName == fullyQualifiedName);
}
}
}