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

Move to Nullean.Xunit.Partitions #63

Merged
merged 5 commits into from
Apr 5, 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
5 changes: 2 additions & 3 deletions examples/Elastic.Xunit.ExampleComplex/Setup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@ public class MyRunOptions : ElasticXunitRunOptions
{
public MyRunOptions()
{
ClusterFilter = "";
RunUnitTests = false;
RunUnitTests = true;
RunIntegrationTests = true;
IntegrationTestsMayUseAlreadyRunningNode = true;
Version = TestVersion;
}

public static ElasticVersion TestVersion { get; } = "8.0.0-SNAPSHOT";
public static ElasticVersion TestVersion { get; } = "latest-8";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

namespace Elastic.Xunit.ExampleComplex
{
[IntegrationTestCluster(typeof(TestCluster))]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewing note: this removes support for marking clusters through attributes. In the spirit of simplifying marking test classes with IClusterFixture<> is now the sole way to get clusters injected.

This was introduced in: elastic/elasticsearch-net#3424

Since this was not ported to the new tests for the new client I think its safe to remove.

[SkipVersion("<6.3.0", "")]
public class TestWithoutClusterFixture
{
Expand Down
90 changes: 67 additions & 23 deletions examples/Elastic.Xunit.ExampleComplex/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information

using Elastic.Elasticsearch.Managed;
using Elastic.Elasticsearch.Xunit.XunitPlumbing;
using Elasticsearch.Net;
using FluentAssertions;
using Xunit;

namespace Elastic.Xunit.ExampleComplex
{
public class MyTestClass : ClusterTestClassBase<TestCluster>
{
public MyTestClass(TestCluster cluster) : base(cluster)
{
}
public MyTestClass(TestCluster cluster) : base(cluster) { }

[I]
public void SomeTest()
Expand All @@ -22,34 +22,70 @@ public void SomeTest()
info.IsValid.Should().BeTrue();

Client.CreateIndex("INASda");
Client.LowLevel.Search<StringResponse>(PostData.Serializable(new {query = new {query_string = 1}}));
}
}

public class Tests1 : ClusterTestClassBase<TestCluster>
{
public Tests1(TestCluster cluster) : base(cluster) { }

Client.LowLevel.Search<StringResponse>(PostData.Serializable(new {query = new {query_string = 1}}));
[U] public void Unit1Test() => (1 + 1).Should().Be(2);
[U] public void Unit1Test1() => (1 + 1).Should().Be(2);
[U] public void Unit1Test2() => (1 + 1).Should().Be(2);
[U] public void Unit1Test3() => (1 + 1).Should().Be(2);
[U] public void Unit1Test4() => (1 + 1).Should().Be(2);
[U] public void Unit1Test5() => (1 + 1).Should().Be(2);
[U] public void Unit1Test6() => (1 + 1).Should().Be(2);
}

public class Tests3
{
[U] public void Unit3Test() => (1 + 1).Should().Be(2);
[U] public void Unit3Test1() => (1 + 1).Should().Be(2);
[U] public void Unit3Test2() => (1 + 1).Should().Be(2);
[U] public void Unit3Test3() => (1 + 1).Should().Be(2);
[U] public void Unit3Test4() => (1 + 1).Should().Be(2);
[U] public void Unit3Test5() => (1 + 1).Should().Be(2);
[U] public void Unit3Test6() => (1 + 1).Should().Be(2);
}

public class Tests2 : ClusterTestClassBase<TestCluster>
{
public Tests2(TestCluster cluster) : base(cluster) { }

[U] public void Unit2Test() => (1 + 1).Should().Be(2);
[U] public void Unit2Test1() => (1 + 1).Should().Be(2);
[U] public void Unit2Test2() => (1 + 1).Should().Be(2);
[U] public void Unit2Test3() => (1 + 1).Should().Be(2);
[U] public void Unit2Test4() => (1 + 1).Should().Be(2);
[U] public void Unit2Test5() => (1 + 1).Should().Be(2);
[U] public void Unit2Test6() => (1 + 1).Should().Be(2);
}

public class MyGenericTestClass : ClusterTestClassBase<TestGenericCluster>
{
public MyGenericTestClass(TestGenericCluster cluster) : base(cluster) { }

[I] public void SomeTest()
{
var info = Client.RootNodeInfo();

info.IsValid.Should().BeTrue();
}
[U] public void MyGenericUnitTest() => (1 + 1).Should().Be(2);
[U] public void MyGenericUnitTest1() => (1 + 1).Should().Be(2);
[U] public void MyGenericUnitTest2() => (1 + 1).Should().Be(2);
[U] public void MyGenericUnitTest3() => (1 + 1).Should().Be(2);
[U] public void MyGenericUnitTest4() => (1 + 1).Should().Be(2);
[U] public void MyGenericUnitTest5() => (1 + 1).Should().Be(2);
[U] public void MyGenericUnitTest6() => (1 + 1).Should().Be(2);
}
//
// public class MyGenericTestClass : ClusterTestClassBase<TestGenericCluster>
// {
// public MyGenericTestClass(TestGenericCluster cluster) : base(cluster) { }
//
// [I] public void SomeTest()
// {
// var info = this.Client.RootNodeInfo();
//
// info.IsValid.Should().BeTrue();
// }
// [U] public void UnitTest()
// {
// (1 + 1).Should().Be(2);
// }
// }

[SkipVersion("<6.2.0", "")]
public class SkipTestClass : ClusterTestClassBase<TestGenericCluster>
{
public SkipTestClass(TestGenericCluster cluster) : base(cluster)
{
}
public SkipTestClass(TestGenericCluster cluster) : base(cluster) { }

[I]
public void SomeTest()
Expand All @@ -62,4 +98,12 @@ public void SomeTest()
[U]
public void UnitTest() => (1 + 1).Should().Be(2);
}

public class DirectInterfaceTests : IClusterFixture<TestGenericCluster>
{
public DirectInterfaceTests(TestGenericCluster cluster) { }

[U]
public void DirectUnitTest() => (1 + 1).Should().Be(2);
}
}
2 changes: 1 addition & 1 deletion examples/Elastic.Xunit.ExampleMinimal/ExampleTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class MyTestCluster : XunitClusterBase
/// We pass our configuration instance to the base class.
/// We only configure it to run version 6.2.3 here but lots of additional options are available.
/// </summary>
public MyTestCluster() : base(new XunitClusterConfiguration("8.0.0-SNAPSHOT") { })
public MyTestCluster() : base(new XunitClusterConfiguration("latest-8") { })
{
}
}
Expand Down
8 changes: 4 additions & 4 deletions examples/ScratchPad/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public static class Program

public static int Main()
{
//ResolveVersions();
ManualConfigRun();
ResolveVersions();
//ManualConfigRun();
//ValidateCombinations.Run();
return 0;
}
Expand Down Expand Up @@ -87,8 +87,8 @@ private static void ResolveVersions()
{
var versions = new[]
{
"8.0.0-SNAPSHOT", "7.0.0-beta1", "6.6.1", "latest-7", "latest", "7.0.0", "7.4.0-SNAPSHOT",
"957e3089:7.2.0", "latest-6"
"latest-8", "7.0.0-beta1", "6.6.1", "latest-7", "latest", "7.0.0", "7.4.0-SNAPSHOT",
"957e3089:7.2.0"
};
//versions = new[] {"latest-7"};
var products = new Product[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
<PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard2.1;net462</TargetFrameworks>
<IsPackable>True</IsPackable>
<IsTestProject>False</IsTestProject>
<Description>Provides an Xunit test framework allowing you to run integration tests against local ephemeral Elasticsearch clusters</Description>
<PackageTags>elastic,elasticsearch,xunit,cluster,integration,test,ephemeral</PackageTags>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Nullean.Xunit.Partitions" Version="0.5.0" />
<PackageReference Include="xunit" Version="2.4.1" />
</ItemGroup>
<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,20 @@
// See the LICENSE file in the project root for more information

using System;
using Nullean.Xunit.Partitions;

namespace Elastic.Elasticsearch.Xunit
{
/// <summary>
/// An assembly attribute that specifies the <see cref="ElasticXunitRunOptions" />
/// for Xunit tests within the assembly.
/// </summary>
[AttributeUsage(AttributeTargets.Assembly)]
public class ElasticXunitConfigurationAttribute : Attribute
{
/// <summary>
/// Creates a new instance of <see cref="ElasticXunitConfigurationAttribute" />
/// </summary>
/// <param name="type">
/// A type deriving from <see cref="ElasticXunitRunOptions" /> that specifies the run options
/// </param>
public ElasticXunitConfigurationAttribute(Type type)
{
var options = Activator.CreateInstance(type) as ElasticXunitRunOptions;
Options = options ?? new ElasticXunitRunOptions();
}
namespace Elastic.Elasticsearch.Xunit;

/// <summary>
/// The run options
/// </summary>
public ElasticXunitRunOptions Options { get; }
}
/// <summary>
/// An assembly attribute that specifies the <see cref="ElasticXunitRunOptions" />
/// for Xunit tests within the assembly.
/// </summary>
[AttributeUsage(AttributeTargets.Assembly)]
public class ElasticXunitConfigurationAttribute : PartitionOptionsAttribute
{
/// <summary>Creates a new instance of <see cref="ElasticXunitConfigurationAttribute" />.</summary>
/// <param name="type">
/// A type deriving from <see cref="ElasticXunitRunOptions" /> that specifies the run options
/// </param>
public ElasticXunitConfigurationAttribute(Type type) : base(type) { }
}
88 changes: 68 additions & 20 deletions src/Elastic.Elasticsearch.Xunit/ElasticXunitRunOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@
// See the LICENSE file in the project root for more information

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.Serialization;
using Elastic.Elasticsearch.Xunit.XunitPlumbing;
using Elastic.Stack.ArtifactsApi;
using Nullean.Xunit.Partitions;
using Xunit.Abstractions;
using static System.StringSplitOptions;

namespace Elastic.Elasticsearch.Xunit
{
/// <summary>
/// The Xunit test runner options
/// </summary>
public class ElasticXunitRunOptions
public class ElasticXunitRunOptions : PartitionOptions
{
/// <summary>
/// Informs the runner whether we expect to run integration tests. Defaults to <c>true</c>
Expand All @@ -35,39 +37,85 @@ public class ElasticXunitRunOptions
public bool RunUnitTests { get; set; }

/// <summary>
/// A global test filter that can be used to only run certain tests.
/// A global cluster filter that can be used to only run certain cluster's tests.
/// Accepts a comma separated list of filters
/// </summary>
public string TestFilter { get; set; }
[Obsolete("Use PartitionFilterRegex instead", false)]
[IgnoreDataMember]
public string ClusterFilter
{
get => PartitionFilterRegex;
set
{
if (string.IsNullOrWhiteSpace(value)) PartitionFilterRegex = value;
else
{
//attempt at being backwards compatible with old way of filtering
var re = string.Join("|", value.Split(new[] { ','}, RemoveEmptyEntries).Select(s => s.Trim()));
PartitionFilterRegex = re;
}
}
}

/// <summary>
/// A global cluster filter that can be used to only run certain cluster's tests.
/// A global test filter that can be used to only run certain cluster's tests.
/// Accepts a comma separated list of filters
/// </summary>
public string ClusterFilter { get; set; }
[Obsolete("Use ParitionFilterRegex instead", false)]
[IgnoreDataMember]
public string TestFilter
{
get => TestFilterRegex;
set
{
if (string.IsNullOrWhiteSpace(value)) TestFilterRegex = value;
else
{
//attempt at being backwards compatible with old way of filtering
var re = string.Join("|", value.Split(new[] { ','}, RemoveEmptyEntries).Select(s => s.Trim()));
TestFilterRegex = re;
}
}
}

/// <summary>
/// Informs the runner what version of Elasticsearch is under test. Required for
/// <see cref="SkipVersionAttribute" /> to kick in
/// </summary>
public ElasticVersion Version { get; set; }

/// <summary>
/// Called when the tests have finished running successfully
/// </summary>
/// <param name="runnerClusterTotals">Per cluster timings of the total test time, including starting Elasticsearch</param>
/// <param name="runnerFailedCollections">All collection of failed cluster, failed tests tuples</param>
public virtual void OnTestsFinished(Dictionary<string, Stopwatch> runnerClusterTotals,
ConcurrentBag<Tuple<string, string>> runnerFailedCollections)
public override void SetOptions(ITestFrameworkDiscoveryOptions discoveryOptions)
{
base.SetOptions(discoveryOptions);
discoveryOptions.SetValue(nameof(Version), Version);
discoveryOptions.SetValue(nameof(RunIntegrationTests), RunIntegrationTests);
discoveryOptions.SetValue(
nameof(IntegrationTestsMayUseAlreadyRunningNode),
IntegrationTestsMayUseAlreadyRunningNode
);
discoveryOptions.SetValue(nameof(RunUnitTests), RunUnitTests);
#pragma warning disable CS0618 // Type or member is obsolete
discoveryOptions.SetValue(nameof(TestFilter), TestFilter);
discoveryOptions.SetValue(nameof(ClusterFilter), ClusterFilter);
#pragma warning restore CS0618 // Type or member is obsolete
}

/// <summary>
/// Called before tests run. An ideal place to perform actions such as writing information to
/// <see cref="Console" />.
/// </summary>
public virtual void OnBeforeTestsRun()
public override void SetOptions(ITestFrameworkExecutionOptions executionOptions)
{

base.SetOptions(executionOptions);
executionOptions.SetValue(nameof(Version), Version);
executionOptions.SetValue(nameof(RunIntegrationTests), RunIntegrationTests);
executionOptions.SetValue(
nameof(IntegrationTestsMayUseAlreadyRunningNode),
IntegrationTestsMayUseAlreadyRunningNode
);
executionOptions.SetValue(nameof(RunUnitTests), RunUnitTests);
#pragma warning disable CS0618 // Type or member is obsolete
executionOptions.SetValue(nameof(TestFilter), TestFilter);
executionOptions.SetValue(nameof(ClusterFilter), ClusterFilter);
#pragma warning restore CS0618 // Type or member is obsolete

}
}
}
Loading
Loading