-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for NUnit3 (Lifecycle) (#237)
- Loading branch information
1 parent
acf6108
commit dae7a94
Showing
22 changed files
with
240 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
# Frameworks - NUnit 3 (Lifecycle) & NSubstitute | ||
Demonstrates how tests are generated using NUnit 3 (Lifecycle) for the test framework and NSubstitute for the mocking framework | ||
|
||
### Source Type(s) | ||
``` csharp | ||
public interface IDependency | ||
{ | ||
int Method(); | ||
} | ||
|
||
public class TestClass | ||
{ | ||
IDependency _dependency; | ||
|
||
public TestClass(IDependency dependency) | ||
{ | ||
_dependency = dependency; | ||
} | ||
|
||
public void SomeMethod(string methodName, int methodValue) | ||
{ | ||
var x = _dependency.Method(); | ||
System.Console.WriteLine("Testing this" + x); | ||
} | ||
|
||
public System.Threading.Tasks.Task<int> SomeAsyncMethod(string methodName, int methodValue) | ||
{ | ||
System.Console.WriteLine("Testing this"); | ||
return System.Threading.Tasks.Task.FromResult(0); | ||
} | ||
} | ||
|
||
``` | ||
|
||
### Generated Tests | ||
``` csharp | ||
[TestFixture, FixtureLifeCycle(LifeCycle.InstancePerTestCase)] | ||
public class TestClassTests | ||
{ | ||
private TestClass _testClass; | ||
private IDependency _dependency; | ||
|
||
public TestClassTests() | ||
{ | ||
_dependency = Substitute.For<IDependency>(); | ||
_testClass = new TestClass(_dependency); | ||
} | ||
|
||
[Test] | ||
public void CanConstruct() | ||
{ | ||
// Act | ||
var instance = new TestClass(_dependency); | ||
|
||
// Assert | ||
Assert.That(instance, Is.Not.Null); | ||
} | ||
|
||
[Test] | ||
public void CannotConstructWithNullDependency() | ||
{ | ||
Assert.Throws<ArgumentNullException>(() => new TestClass(default(IDependency))); | ||
} | ||
|
||
[Test] | ||
public void CanCallSomeMethod() | ||
{ | ||
// Arrange | ||
var methodName = "TestValue237820880"; | ||
var methodValue = 1002897798; | ||
|
||
_dependency.Method().Returns(534011718); | ||
|
||
// Act | ||
_testClass.SomeMethod(methodName, methodValue); | ||
|
||
// Assert | ||
_dependency.Received().Method(); | ||
|
||
Assert.Fail("Create or modify test"); | ||
} | ||
|
||
[TestCase(null)] | ||
[TestCase("")] | ||
[TestCase(" ")] | ||
public void CannotCallSomeMethodWithInvalidMethodName(string value) | ||
{ | ||
Assert.Throws<ArgumentNullException>(() => _testClass.SomeMethod(value, 1657007234)); | ||
} | ||
|
||
[Test] | ||
public async Task CanCallSomeAsyncMethod() | ||
{ | ||
// Arrange | ||
var methodName = "TestValue1412011072"; | ||
var methodValue = 929393559; | ||
|
||
// Act | ||
var result = await _testClass.SomeAsyncMethod(methodName, methodValue); | ||
|
||
// Assert | ||
Assert.Fail("Create or modify test"); | ||
} | ||
|
||
[TestCase(null)] | ||
[TestCase("")] | ||
[TestCase(" ")] | ||
public void CannotCallSomeAsyncMethodWithInvalidMethodName(string value) | ||
{ | ||
Assert.ThrowsAsync<ArgumentNullException>(() => _testClass.SomeAsyncMethod(value, 760389092)); | ||
} | ||
} | ||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/Unitverse.Core/Frameworks/Test/NUnit3LifeCycleFramework.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
namespace Unitverse.Core.Frameworks.Test | ||
{ | ||
using System.Collections.Generic; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Unitverse.Core.Helpers; | ||
using Unitverse.Core.Options; | ||
|
||
public class NUnit3LifeCycleFramework : NUnit3TestFramework | ||
{ | ||
public NUnit3LifeCycleFramework(IUnitTestGeneratorOptions options) | ||
: base(options) | ||
{ | ||
} | ||
|
||
public override IEnumerable<AttributeSyntax> TestClassAttributes => new[] | ||
{ | ||
Generate.Attribute("TestFixture"), | ||
Generate.Attribute("FixtureLifeCycle", Generate.MemberAccess("LifeCycle", "InstancePerTestCase")), | ||
}; | ||
|
||
protected override BaseMethodDeclarationSyntax CreateSetupMethodSyntax(string targetTypeName) | ||
{ | ||
return SyntaxFactory.ConstructorDeclaration(SyntaxFactory.Identifier(targetTypeName)).AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.