Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add UnityVersionAttribute #75

Merged
merged 2 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,40 @@ public class MyTestClass
```


#### UnityVersion

`UnityVersionAttribute` is an NUnit test attribute class to skip test run if Unity version is older and/or newer than specified.

This attribute can attached to test method, test class (`TestFixture`) and test assembly.
Can be used with sync Test, async Test, and UnityTest.

Usage:

```csharp
using System;
using NUnit.Framework;
using TestHelper.Attributes;

[TestFixture]
public class MyTestClass
{
[Test]
[UnityVersion(newerThanOrEqual: "2022")]
public void MyTestMethod()
{
// Test run only for Unity 2022.1.0f1 or later.
}

[Test]
[UnityVersion(olderThan: "2019.4.0f1")]
public void MyTestMethod()
{
// Test run only for Unity older than 2019.4.0f1.
}
}
```


#### CreateScene

`CreateSceneAttribute` is an NUnit test attribute class to create new scene before running test.
Expand Down
68 changes: 68 additions & 0 deletions Runtime/Attributes/UnityVersionAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright (c) 2023-2024 Koji Hasegawa.
// This software is released under the MIT License.

using System;
using NUnit.Framework;
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal;
using UnityEngine;

namespace TestHelper.Attributes
{
/// <summary>
/// Skip this test run if Unity version is older and/or newer than specified.
/// </summary>
[AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Method)]
public class UnityVersionAttribute : NUnitAttribute, IApplyToTest
{
private readonly string _newerThanOrEqual;
private readonly string _olderThan;

/// <summary>
/// Skip this test run if Unity version is older and/or newer than specified.
/// Valid format, e.g., "2023.2.16f1", "2023.2", and "2023".
/// </summary>
/// <param name="newerThanOrEqual">This test will run if the Unity editor version is newer than or equal the specified version.</param>
/// <param name="olderThan">This test will run if the Unity editor version is older than the specified version.</param>
public UnityVersionAttribute(string newerThanOrEqual = null, string olderThan = null)
{
_newerThanOrEqual = newerThanOrEqual;
_olderThan = olderThan;
}

public void ApplyToTest(Test test)
{
if (IsSkip(Application.unityVersion))
{
test.RunState = RunState.Ignored;
test.Properties.Set("_SKIPREASON", "Unity editor version is out of range.");
}
}

internal bool IsSkip(string unityVersion)
{
if (!string.IsNullOrEmpty(_newerThanOrEqual) && !IsNewerThanEqual(unityVersion, _newerThanOrEqual))
{
return true;
}

if (!string.IsNullOrEmpty(_olderThan) && !IsOlderThan(unityVersion, _olderThan))
{
return true;
}

return false;
}

private static bool IsNewerThanEqual(string unityVersion, string newerThanEqual)
{
return string.Compare(unityVersion, newerThanEqual, StringComparison.Ordinal) >= 0;
}

private static bool IsOlderThan(string unityVersion, string olderThan)
{
var digits = olderThan.Length; // Evaluate only up to olderThan`s digits
return string.Compare(unityVersion.Substring(0, digits), olderThan, StringComparison.Ordinal) <= 0;
}
}
}
2 changes: 2 additions & 0 deletions Runtime/Attributes/UnityVersionAttribute.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

71 changes: 71 additions & 0 deletions Tests/Runtime/Attributes/UnityVersionAttributeTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright (c) 2023-2024 Koji Hasegawa.
// This software is released under the MIT License.

using NUnit.Framework;

namespace TestHelper.Attributes
{
[TestFixture]
public class UnityVersionAttributeTest
{
private const string UnityVersion = "2023.2.16f1";

[TestCase("2023.2.16f1")] // equal version is not skip
[TestCase("2023.2.15f1")]
[TestCase("2023.2")]
[TestCase("2023")]
[TestCase("2022")]
public void IsSkip_newerThanOrEqual_NotSkip(string newerThanOrEqual)
{
var sut = new UnityVersionAttribute(newerThanOrEqual);
var actual = sut.IsSkip(UnityVersion);
Assert.That(actual, Is.False);
}

[TestCase("2023.2.17f1")]
[TestCase("2023.3")]
[TestCase("6000")]
public void IsSkip_newerThanOrEqual_Skip(string newerThanOrEqual)
{
var sut = new UnityVersionAttribute(newerThanOrEqual);
var actual = sut.IsSkip(UnityVersion);
Assert.That(actual, Is.True);
}

[TestCase("2023.2.17f1")]
[TestCase("2023.2.16f1")] // equal version is not skip
[TestCase("2023.2")]
[TestCase("2023")]
[TestCase("6000")]
public void IsSkip_olderThan_NotSkip(string olderThan)
{
var sut = new UnityVersionAttribute(olderThan: olderThan);
var actual = sut.IsSkip(UnityVersion);
Assert.That(actual, Is.False);
}

[TestCase("2023.2.15f1")]
[TestCase("2023.1")]
[TestCase("2022")]
[TestCase("2021")]
public void IsSkip_olderThan_Skip(string olderThan)
{
var sut = new UnityVersionAttribute(olderThan: olderThan);
var actual = sut.IsSkip(UnityVersion);
Assert.That(actual, Is.True);
}

[Test]
[UnityVersion("2019")]
public void Attach_newerThanOrEqual2019_NotSkip()
{
}

[Test]
[UnityVersion(olderThan: "2019.4.0f1")]
public void Attach_olderThan2019_4_0f1_Skip()
{
Assert.Fail("This test should be skipped.");
}
}
}
2 changes: 2 additions & 0 deletions Tests/Runtime/Attributes/UnityVersionAttributeTest.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.