1
// *********************************************************************
2
// Copyright 2007, Andreas Schlapsi
3
// This is free software licensed under the MIT license.
4
// *********************************************************************
6
using System.Reflection;
8
using NUnit.Core.Extensibility;
10
using NUnit.Framework.SyntaxHelpers;
15
namespace NUnitExtension.RowTest.AddIn.UnitTests
18
public class RowTestAddInTest : BaseTestFixture
20
private Test dummy = null; // OK unless builder starts using the suite argument
23
private NMock2.Mockery _mocks;
28
_mocks = new NMock2.Mockery();
32
public void Install_Successful()
35
IExtensionHost extensionHostMock = (IExtensionHost)_mocks.NewMock(typeof(IExtensionHost));
36
IExtensionPoint extensionPointMock = (IExtensionPoint)_mocks.NewMock(typeof(IExtensionPoint));
37
RowTestAddIn addIn = new RowTestAddIn();
39
NMock2.Expect.Once.On(extensionHostMock)
40
.Method("GetExtensionPoint").With("TestCaseBuilders")
41
.Will(NMock2.Return.Value(extensionPointMock));
43
NMock2.Expect.Once.On(extensionPointMock)
44
.Method("Install").With(addIn);
46
bool installed = addIn.Install(extensionHost);
48
_mocks.VerifyAllExpectationsHaveBeenMet();
49
Assert.That(installed, Is.True);
51
DynamicMock extensionHostMock = new DynamicMock(typeof(IExtensionHost));
52
IExtensionHost extensionHost = (IExtensionHost)extensionHostMock.MockInstance;
53
DynamicMock extensionPointMock = new DynamicMock(typeof(IExtensionPoint));
54
IExtensionPoint extensionPoint = (IExtensionPoint)extensionPointMock.MockInstance;
55
RowTestAddIn addIn = new RowTestAddIn();
57
extensionHostMock.ExpectAndReturn("GetExtensionPoint", extensionPointMock.MockInstance, "TestCaseBuilders");
58
extensionPointMock.Expect("Install", addIn);
60
bool installed = addIn.Install(extensionHost);
62
extensionPointMock.Verify();
63
extensionHostMock.Verify();
64
Assert.That(installed, Is.True);
69
public void Install_Failure()
72
IExtensionHost extensionHostMock = (IExtensionHost)_mocks.NewMock(typeof(IExtensionHost));
73
RowTestAddIn addIn = new RowTestAddIn();
75
NMock2.Expect.Once.On(extensionHostMock)
76
.Method("GetExtensionPoint").With("TestCaseBuilders")
77
.Will(NMock2.Return.Value(null));
79
bool installed = addIn.Install(extensionHostMock);
81
_mocks.VerifyAllExpectationsHaveBeenMet();
82
Assert.That(installed, Is.False);
84
DynamicMock extensionHostMock = new DynamicMock(typeof(IExtensionHost));
85
IExtensionHost extensionHost = (IExtensionHost) extensionHostMock.MockInstance;
86
RowTestAddIn addIn = new RowTestAddIn();
88
extensionHostMock.ExpectAndReturn("GetExtensionPoint",null,"TestCaseBuilders");
90
bool installed = addIn.Install(extensionHost);
92
extensionHostMock.Verify();
93
Assert.That(installed, Is.False);
98
public void CanBuildFrom_False()
100
RowTestAddIn addIn = new RowTestAddIn();
101
MethodInfo method = GetTestClassMethod("MethodWithoutRowTestAttribute");
103
bool canBuildFrom = addIn.CanBuildFrom(method);
105
Assert.That(canBuildFrom, Is.False);
109
public void CanBuildFrom_True()
111
RowTestAddIn addIn = new RowTestAddIn();
112
MethodInfo method = GetTestClassMethod("MethodWithRowTestAttribute");
114
bool canBuildFrom = addIn.CanBuildFrom(method);
116
Assert.That(canBuildFrom, Is.True);
120
public void BuildFrom_WithoutRows()
122
RowTestAddIn addIn = new RowTestAddIn();
123
MethodInfo method = GetTestClassMethod("MethodWithRowTestAttribute");
125
Test test = addIn.BuildFrom(method);
127
Assert.That(test, Is.InstanceOfType(typeof(RowTestSuite)));
128
RowTestSuite suite = (RowTestSuite) test;
130
Assert.That(suite.TestCount, Is.EqualTo(0));
134
public void BuildFrom_WithRows()
136
RowTestAddIn addIn = new RowTestAddIn();
137
MethodInfo method = GetRowTestMethodWith2Rows();
139
Test test = addIn.BuildFrom(method);
141
Assert.That(test, Is.InstanceOfType(typeof(RowTestSuite)));
142
RowTestSuite suite = (RowTestSuite) test;
144
Assert.That(suite.TestCount, Is.EqualTo(2));
145
Assert.That(suite.Tests[0], Is.InstanceOfType(typeof(RowTestCase)));
146
Assert.That(suite.Tests[1], Is.InstanceOfType(typeof(RowTestCase)));
148
RowTestCase testCase1 = (RowTestCase) suite.Tests[0];
149
RowTestCase testCase2 = (RowTestCase) suite.Tests[1];
151
Assert.That(testCase1.Arguments.Length, Is.EqualTo(2));
152
Assert.That(testCase2.Arguments.Length, Is.EqualTo(2));
156
public void BuildFrom_WithTestName()
158
RowTestAddIn addIn = new RowTestAddIn();
159
MethodInfo method = GetRowTestMethodWithTestName();
161
Test test = addIn.BuildFrom(method);
163
Assert.That(test, Is.InstanceOfType(typeof(RowTestSuite)));
164
RowTestSuite suite = (RowTestSuite) test;
166
Assert.That(suite.TestCount, Is.EqualTo(1));
167
Assert.That(suite.Tests[0], Is.InstanceOfType(typeof(RowTestCase)));
169
RowTestCase testCase = (RowTestCase) suite.Tests[0];
171
Assert.That(testCase.TestName.Name, Is.EqualTo("UnitTest(4, 5)"));
175
public void BuildFrom_WithExpectedException()
177
RowTestAddIn addIn = new RowTestAddIn();
178
MethodInfo method = GetRowTestMethodWithExpectedException();
180
Test test = addIn.BuildFrom(method);
182
Assert.That(test, Is.InstanceOfType(typeof(RowTestSuite)));
183
RowTestSuite suite = (RowTestSuite) test;
185
Assert.That(suite.TestCount, Is.EqualTo(1));
186
Assert.That(suite.Tests[0], Is.InstanceOfType(typeof(RowTestCase)));
188
RowTestCase testCase = (RowTestCase) suite.Tests[0];
190
Assert.That(testCase.ExceptionExpected, Is.True);
191
Assert.That(testCase.ExpectedExceptionType, Is.EqualTo(typeof(InvalidOperationException)));
195
public void BuildFrom_WithExpectedExceptionAndExceptionMessage()
197
RowTestAddIn addIn = new RowTestAddIn();
198
MethodInfo method = GetRowTestMethodWithExpectedExceptionAndExceptionMessage();
200
Test test = addIn.BuildFrom(method);
202
Assert.That(test, Is.InstanceOfType(typeof(RowTestSuite)));
203
RowTestSuite suite = (RowTestSuite) test;
205
Assert.That(suite.TestCount, Is.EqualTo(1));
206
Assert.That(suite.Tests[0], Is.InstanceOfType(typeof(RowTestCase)));
208
RowTestCase testCase = (RowTestCase) suite.Tests[0];
210
Assert.That(testCase.ExceptionExpected, Is.True);
211
Assert.That(testCase.ExpectedExceptionType, Is.EqualTo(typeof(InvalidOperationException)));
212
Assert.That(testCase.ExpectedMessage, Is.EqualTo("Expected Exception Message."));
216
public void BuildFrom_SetsNameCorrectly()
218
RowTestAddIn addIn = new RowTestAddIn();
219
Type testClass = typeof(TestClass);
220
MethodInfo method = testClass.GetMethod(Method_RowTestMethodWith2Rows, BindingFlags.Public | BindingFlags.Instance);
222
Test test = addIn.BuildFrom(method);
224
Assert.That(test, Is.InstanceOfType(typeof(RowTestSuite)));
225
RowTestSuite suite = (RowTestSuite) test;
227
Assert.That(suite.TestName.Name, Is.EqualTo(method.Name));
228
Assert.That(suite.TestName.FullName, Is.EqualTo(testClass.FullName + "." + method.Name));
232
public void BuildFrom_SetsCommonNUnitAttributes()
234
RowTestAddIn addin = new RowTestAddIn();
235
MethodInfo method = GetTestClassMethod("RowTestMethodWithCategory");
237
Test test = addin.BuildFrom(method);
239
Assert.That(test.Categories, Is.Not.Null);
240
Assert.That(test.Categories.Count, Is.EqualTo(1));
241
Assert.That(test.Categories[0], Is.EqualTo("Category"));
245
public void BuildFrom_WithSpecialValueNull()
247
RowTestAddIn addIn = new RowTestAddIn();
248
MethodInfo method = GetTestClassMethod("RowTestMethodWithSpecialValue");
250
Test test = addIn.BuildFrom(method);
252
Assert.That(test, Is.InstanceOfType(typeof(RowTestSuite)));
253
RowTestSuite suite = (RowTestSuite) test;
255
Assert.That(suite.TestCount, Is.EqualTo(1));
256
Assert.That(suite.Tests[0], Is.InstanceOfType(typeof(RowTestCase)));
258
RowTestCase testCase = (RowTestCase) suite.Tests[0];
260
Assert.That(testCase.Arguments.Length, Is.EqualTo(1));
261
Assert.That(testCase.Arguments[0], Is.Null);
265
public void BuildFrom_WithNull()
267
RowTestAddIn addIn = new RowTestAddIn();
268
MethodInfo method = GetTestClassMethod("RowTestMethodWithNullArgument");
270
Test test = addIn.BuildFrom(method);
272
Assert.That(test, Is.InstanceOfType(typeof(RowTestSuite)));
273
RowTestSuite suite = (RowTestSuite) test;
275
Assert.That(suite.TestCount, Is.EqualTo(1));
276
Assert.That(suite.Tests[0], Is.InstanceOfType(typeof(RowTestCase)));
278
RowTestCase testCase = (RowTestCase) suite.Tests[0];
280
Assert.That(testCase.Arguments, Is.Null);