forked from microsoft/vstest
/
DiscoveryManagerTests.cs
227 lines (179 loc) · 9.33 KB
/
DiscoveryManagerTests.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
namespace TestPlatform.CrossPlatEngine.UnitTests.Discovery
{
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using Microsoft.VisualStudio.TestPlatform.Common.ExtensionFramework;
using Microsoft.VisualStudio.TestPlatform.Common.Telemetry;
using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Discovery;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;
using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers.Interfaces;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using TestPlatform.Common.UnitTests.ExtensionFramework;
using CrossPlatEngineResources = Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Resources.Resources;
[TestClass]
public class DiscoveryManagerTests
{
private DiscoveryManager discoveryManager;
private Mock<IRequestData> mockRequestData;
private Mock<IMetricsCollection> mockMetricsCollection;
[TestInitialize]
public void TestInit()
{
this.mockRequestData = new Mock<IRequestData>();
this.mockMetricsCollection = new Mock<IMetricsCollection>();
this.mockRequestData.Setup(rd => rd.MetricsCollection).Returns(this.mockMetricsCollection.Object);
this.discoveryManager = new DiscoveryManager(this.mockRequestData.Object);
}
[TestCleanup]
public void TestCleanup()
{
TestDiscoveryExtensionManager.Destroy();
TestPluginCache.Instance = null;
}
#region Initialize tests
[TestMethod]
public void InitializeShouldUpdateAdditionalExtenions()
{
var mockFileHelper = new Mock<IFileHelper>();
var mockLogger = new Mock<ITestDiscoveryEventsHandler2>();
mockFileHelper.Setup(fh => fh.DirectoryExists(It.IsAny<string>())).Returns(false);
TestPluginCache.Instance = new TestableTestPluginCache();
this.discoveryManager.Initialize(
new string[] { typeof(TestPluginCacheTests).GetTypeInfo().Assembly.Location }, mockLogger.Object);
var allDiscoverers = TestDiscoveryExtensionManager.Create().Discoverers;
Assert.IsNotNull(allDiscoverers);
Assert.IsTrue(allDiscoverers.Count() > 0);
}
#endregion
#region DiscoverTests tests
[TestMethod]
public void DiscoverTestsShouldLogIfTheSourceDoesNotExist()
{
var criteria = new DiscoveryCriteria(new List<string> { "imaginary.dll" }, 100, null);
var mockLogger = new Mock<ITestDiscoveryEventsHandler2>();
this.discoveryManager.DiscoverTests(criteria, mockLogger.Object);
var errorMessage = string.Format(CultureInfo.CurrentCulture, "Could not find file {0}.", Path.Combine(Directory.GetCurrentDirectory(), "imaginary.dll"));
mockLogger.Verify(
l =>
l.HandleLogMessage(
Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging.TestMessageLevel.Warning,
errorMessage),
Times.Once);
}
[TestMethod]
public void DiscoverTestsShouldLogIfThereAreNoValidSources()
{
var sources = new List<string> { "imaginary.dll" };
var criteria = new DiscoveryCriteria(sources, 100, null);
var mockLogger = new Mock<ITestDiscoveryEventsHandler2>();
this.discoveryManager.DiscoverTests(criteria, mockLogger.Object);
var sourcesString = string.Join(",", sources.ToArray());
var errorMessage = string.Format(CultureInfo.CurrentCulture, CrossPlatEngineResources.NoValidSourceFoundForDiscovery, sourcesString);
mockLogger.Verify(
l =>
l.HandleLogMessage(
Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging.TestMessageLevel.Warning,
errorMessage),
Times.Once);
}
[TestMethod]
public void DiscoverTestsShouldLogIfTheSameSourceIsSpecifiedTwice()
{
TestPluginCacheTests.SetupMockExtensions(
new string[] { typeof(DiscovererEnumeratorTests).GetTypeInfo().Assembly.Location },
() => { });
var sources = new List<string>
{
typeof(DiscoveryManagerTests).GetTypeInfo().Assembly.Location,
typeof(DiscoveryManagerTests).GetTypeInfo().Assembly.Location
};
var criteria = new DiscoveryCriteria(sources, 100, null);
var mockLogger = new Mock<ITestDiscoveryEventsHandler2>();
this.discoveryManager.DiscoverTests(criteria, mockLogger.Object);
var errorMessage = string.Format(CultureInfo.CurrentCulture, CrossPlatEngineResources.DuplicateSource, sources[0]);
mockLogger.Verify(
l =>
l.HandleLogMessage(
Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging.TestMessageLevel.Warning,
errorMessage),
Times.Once);
}
[TestMethod]
public void DiscoverTestsShouldDiscoverTestsInTheSpecifiedSource()
{
TestPluginCacheTests.SetupMockExtensions(
new string[] { typeof(DiscovererEnumeratorTests).GetTypeInfo().Assembly.Location },
() => { });
var sources = new List<string>
{
typeof(DiscoveryManagerTests).GetTypeInfo().Assembly.Location
};
var criteria = new DiscoveryCriteria(sources, 1, null);
var mockLogger = new Mock<ITestDiscoveryEventsHandler2>();
this.discoveryManager.DiscoverTests(criteria, mockLogger.Object);
// Assert that the tests are passed on via the handletestruncomplete event.
mockLogger.Verify(l => l.HandleDiscoveryComplete(It.IsAny<DiscoveryCompleteEventArgs>(), It.IsAny<IEnumerable<TestCase>>()), Times.Once);
}
[TestMethod]
public void DiscoverTestsShouldSendMetricsOnDiscoveryComplete()
{
var metricsCollector = new MetricsCollection();
metricsCollector.Add("DummyMessage", "DummyValue");
this.mockRequestData.Setup(rd => rd.MetricsCollection).Returns(metricsCollector);
DiscoveryCompleteEventArgs receivedDiscoveryCompleteEventArgs = null;
TestPluginCacheTests.SetupMockExtensions(
new string[] { typeof(DiscovererEnumeratorTests).GetTypeInfo().Assembly.Location },
() => { });
var sources = new List<string>
{
typeof(DiscoveryManagerTests).GetTypeInfo().Assembly.Location
};
var mockLogger = new Mock<ITestDiscoveryEventsHandler2>();
var criteria = new DiscoveryCriteria(sources, 1, null);
mockLogger.Setup(ml => ml.HandleDiscoveryComplete(It.IsAny<DiscoveryCompleteEventArgs>(), It.IsAny<IEnumerable<TestCase>>()))
.Callback(
(DiscoveryCompleteEventArgs complete,
IEnumerable<TestCase> tests) =>
{
receivedDiscoveryCompleteEventArgs = complete;
});
// Act.
this.discoveryManager.DiscoverTests(criteria, mockLogger.Object);
// Assert
Assert.IsNotNull(receivedDiscoveryCompleteEventArgs.Metrics);
Assert.IsTrue(receivedDiscoveryCompleteEventArgs.Metrics.Any());
Assert.IsTrue(receivedDiscoveryCompleteEventArgs.Metrics.ContainsKey("DummyMessage"));
}
[TestMethod]
public void DiscoverTestsShouldCollectMetrics()
{
var mockMetricsCollector = new Mock<IMetricsCollection>();
var dict = new Dictionary<string, object>();
dict.Add("DummyMessage", "DummyValue");
mockMetricsCollector.Setup(mc => mc.Metrics).Returns(dict);
this.mockRequestData.Setup(rd => rd.MetricsCollection).Returns(mockMetricsCollector.Object);
TestPluginCacheTests.SetupMockExtensions(
new string[] { typeof(DiscovererEnumeratorTests).GetTypeInfo().Assembly.Location },
() => { });
var sources = new List<string>
{
typeof(DiscoveryManagerTests).GetTypeInfo().Assembly.Location
};
var mockLogger = new Mock<ITestDiscoveryEventsHandler2>();
var criteria = new DiscoveryCriteria(sources, 1, null);
// Act.
this.discoveryManager.DiscoverTests(criteria, mockLogger.Object);
// Verify.
mockMetricsCollector.Verify(rd => rd.Add(TelemetryDataConstants.DiscoveryState, It.IsAny<string>()), Times.Once);
mockMetricsCollector.Verify(rd => rd.Add(TelemetryDataConstants.TotalTestsDiscovered, It.IsAny<object>()), Times.Once);
}
#endregion
}
}