-
Notifications
You must be signed in to change notification settings - Fork 722
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3744 from gleb-osokin/master
Apply FixtureLifeCycle to parametrized tests
- Loading branch information
Showing
7 changed files
with
442 additions
and
48 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
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
42 changes: 42 additions & 0 deletions
42
src/NUnitFramework/framework/Internal/Extensions/TestExtensions.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,42 @@ | ||
// *********************************************************************** | ||
// Copyright (c) 2021 Charlie Poole, Rob Prouse | ||
// | ||
// 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 NUnit.Framework.Interfaces; | ||
|
||
namespace NUnit.Framework.Internal.Extensions | ||
{ | ||
internal static class TestExtensions | ||
{ | ||
public static bool HasLifeCycle(this ITest test, LifeCycle lifeCycle) | ||
{ | ||
while (test != null) | ||
{ | ||
if (test is TestFixture fixture && fixture.LifeCycle == LifeCycle.InstancePerTestCase) | ||
return lifeCycle == LifeCycle.InstancePerTestCase; | ||
|
||
test = test.Parent; | ||
} | ||
return lifeCycle != LifeCycle.InstancePerTestCase; | ||
} | ||
} | ||
} |
179 changes: 179 additions & 0 deletions
179
src/NUnitFramework/testdata/AssemblyLevelLifeCycleFixture.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,179 @@ | ||
// *********************************************************************** | ||
// Copyright (c) 2021 Charlie Poole, Rob Prouse | ||
// | ||
// 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. | ||
// *********************************************************************** | ||
|
||
namespace NUnit.TestData.LifeCycleTests | ||
{ | ||
public static class AssemblyLevelFixtureLifeCycleTest | ||
{ | ||
public const string Code = @" | ||
using NUnit.Framework; | ||
[assembly: FixtureLifeCycle(LifeCycle.InstancePerTestCase)] | ||
[TestFixture] | ||
public class FixtureUnderTest | ||
{ | ||
private int _value; | ||
[Test] | ||
public void Test1() | ||
{ | ||
Assert.AreEqual(0, _value++); | ||
} | ||
[Test] | ||
public void Test2() | ||
{ | ||
Assert.AreEqual(0, _value++); | ||
} | ||
} | ||
"; | ||
} | ||
|
||
public static class AssemblyLevelLifeCycleTestFixtureSourceTest | ||
{ | ||
public const string Code = @" | ||
using NUnit.Framework; | ||
[assembly: FixtureLifeCycle(LifeCycle.InstancePerTestCase)] | ||
[TestFixtureSource(""FixtureArgs"")] | ||
public class FixtureUnderTest | ||
{ | ||
private readonly int _initialValue; | ||
private int _value; | ||
public FixtureUnderTest(int num) | ||
{ | ||
_initialValue = num; | ||
_value = num; | ||
} | ||
public static int[] FixtureArgs() | ||
{ | ||
return new int[] { 1, 42 }; | ||
} | ||
[Test] | ||
public void Test1() | ||
{ | ||
Assert.AreEqual(_initialValue, _value++); | ||
} | ||
[Test] | ||
public void Test2() | ||
{ | ||
Assert.AreEqual(_initialValue, _value++); | ||
} | ||
} | ||
"; | ||
} | ||
|
||
public static class AssemblyLevelLifeCycleFixtureWithTestCasesTest | ||
{ | ||
public const string Code = @" | ||
using NUnit.Framework; | ||
[assembly: FixtureLifeCycle(LifeCycle.InstancePerTestCase)] | ||
public class FixtureUnderTest | ||
{ | ||
private int _counter; | ||
[TestCase(0)] | ||
[TestCase(1)] | ||
[TestCase(2)] | ||
[TestCase(3)] | ||
public void Test(int _) | ||
{ | ||
Assert.AreEqual(0, _counter++); | ||
} | ||
} | ||
"; | ||
} | ||
|
||
public static class AssemblyLevelLifeCycleFixtureWithTestCaseSourceTest | ||
{ | ||
public const string Code = @" | ||
using NUnit.Framework; | ||
[assembly: FixtureLifeCycle(LifeCycle.InstancePerTestCase)] | ||
public class FixtureUnderTest | ||
{ | ||
private int _counter; | ||
public static int[] Args() | ||
{ | ||
return new int[] { 1, 42 }; | ||
} | ||
[TestCaseSource(""Args"")] | ||
public void Test(int _) | ||
{ | ||
Assert.AreEqual(0, _counter++); | ||
} | ||
} | ||
"; | ||
} | ||
|
||
public static class AssemblyLevelLifeCycleFixtureWithValuesTest | ||
{ | ||
public const string Code = @" | ||
using NUnit.Framework; | ||
[assembly: FixtureLifeCycle(LifeCycle.InstancePerTestCase)] | ||
public class FixtureUnderTest | ||
{ | ||
private int _counter; | ||
[Test] | ||
public void Test([Values] bool? _) | ||
{ | ||
Assert.AreEqual(0, _counter++); | ||
} | ||
} | ||
"; | ||
} | ||
|
||
public static class AssemblyLevelLifeCycleFixtureWithTheoryTest | ||
{ | ||
public const string Code = @" | ||
using NUnit.Framework; | ||
[assembly: FixtureLifeCycle(LifeCycle.InstancePerTestCase)] | ||
public class FixtureUnderTest | ||
{ | ||
private int _counter; | ||
[Theory] | ||
public void Test(bool? _) | ||
{ | ||
Assert.AreEqual(0, _counter++); | ||
} | ||
} | ||
"; | ||
} | ||
} |
Oops, something went wrong.