From 95b6f2f775238838e68e0117ea901c1c8dd3639a Mon Sep 17 00:00:00 2001 From: Guriy Samarin Date: Sun, 14 Apr 2024 00:12:22 +0100 Subject: [PATCH] changing utest framework to xunit --- csharp/tests/Integration/GetAndSet.cs | 39 ++++++++------ .../tests/Integration/IntegrationTestBase.cs | 54 ++++++++++--------- csharp/tests/Usings.cs | 2 +- csharp/tests/tests.csproj | 18 +++++-- 4 files changed, 65 insertions(+), 48 deletions(-) diff --git a/csharp/tests/Integration/GetAndSet.cs b/csharp/tests/Integration/GetAndSet.cs index 79eb6ec50e..f5f7e72f7a 100644 --- a/csharp/tests/Integration/GetAndSet.cs +++ b/csharp/tests/Integration/GetAndSet.cs @@ -2,19 +2,23 @@ using System.Runtime.InteropServices; +using FluentAssertions; + using Glide; using static Tests.Integration.IntegrationTestBase; namespace Tests.Integration; -public class GetAndSet +public class GetAndSet : IClassFixture { private async Task GetAndSetValues(AsyncClient client, string key, string value) { - string? setResult = await client.SetAsync(key, value); - Assert.That(setResult, Is.EqualTo("OK")); - string? result = await client.GetAsync(key); - Assert.That(result, Is.EqualTo(value)); + _ = (await client.SetAsync(key, value)) + .Should() + .Be("OK"); + _ = (await client.GetAsync(key)) + .Should() + .Be(value); } private async Task GetAndSetRandomValues(AsyncClient client) @@ -24,14 +28,14 @@ private async Task GetAndSetRandomValues(AsyncClient client) await GetAndSetValues(client, key, value); } - [Test] + [Fact] public async Task GetReturnsLastSet() { using AsyncClient client = new("localhost", TestConfiguration.STANDALONE_PORTS[0], false); await GetAndSetRandomValues(client); } - [Test] + [Fact] public async Task GetAndSetCanHandleNonASCIIUnicode() { using AsyncClient client = new("localhost", TestConfiguration.STANDALONE_PORTS[0], false); @@ -40,15 +44,16 @@ public async Task GetAndSetCanHandleNonASCIIUnicode() await GetAndSetValues(client, key, value); } - [Test] + [Fact] public async Task GetReturnsNull() { using AsyncClient client = new("localhost", TestConfiguration.STANDALONE_PORTS[0], false); - string? result = await client.GetAsync(Guid.NewGuid().ToString()); - Assert.That(result, Is.EqualTo(null)); + _ = (await client.GetAsync(Guid.NewGuid().ToString())) + .Should() + .BeNull(); } - [Test] + [Fact] public async Task GetReturnsEmptyString() { using AsyncClient client = new("localhost", TestConfiguration.STANDALONE_PORTS[0], false); @@ -57,13 +62,14 @@ public async Task GetReturnsEmptyString() await GetAndSetValues(client, key, value); } - [Test] + [Fact] public async Task HandleVeryLargeInput() { // TODO invesitage and fix if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) { - Assert.Ignore("Flaky on MacOS"); + //"Flaky on MacOS" + return; } using AsyncClient client = new("localhost", TestConfiguration.STANDALONE_PORTS[0], false); @@ -80,7 +86,7 @@ public async Task HandleVeryLargeInput() // This test is slow and hardly a unit test, but it caught timing and releasing issues in the past, // so it's being kept. - [Test] + [Fact] public void ConcurrentOperationsWork() { // TODO investigate and fix @@ -105,8 +111,9 @@ public void ConcurrentOperationsWork() } else { - string? result = await client.GetAsync(Guid.NewGuid().ToString()); - Assert.That(result, Is.EqualTo(null)); + _ = (await client.GetAsync(Guid.NewGuid().ToString())) + .Should() + .BeNull(); } } })); diff --git a/csharp/tests/Integration/IntegrationTestBase.cs b/csharp/tests/Integration/IntegrationTestBase.cs index c4a87756d0..2f507e0473 100644 --- a/csharp/tests/Integration/IntegrationTestBase.cs +++ b/csharp/tests/Integration/IntegrationTestBase.cs @@ -2,11 +2,13 @@ using System.Diagnostics; +using Xunit.Abstractions; +using Xunit.Sdk; + // Note: All IT should be in the same namespace namespace Tests.Integration; -[SetUpFixture] -public class IntegrationTestBase +public class IntegrationTestBase : IDisposable { internal class TestConfiguration { @@ -15,9 +17,24 @@ internal class TestConfiguration public static Version REDIS_VERSION { get; internal set; } = new(); } - [OneTimeSetUp] - public void SetUp() + private readonly IMessageSink _diagnosticMessageSink; + + public IntegrationTestBase(IMessageSink diagnosticMessageSink) { + _diagnosticMessageSink = diagnosticMessageSink; + string? projectDir = Directory.GetCurrentDirectory(); + while (!(Path.GetFileName(projectDir) == "csharp" || projectDir == null)) + { + projectDir = Path.GetDirectoryName(projectDir); + } + + if (projectDir == null) + { + throw new FileNotFoundException("Can't detect the project dir. Are you running tests from `csharp` directory?"); + } + + _scriptDir = Path.Combine(projectDir, "..", "utils"); + // Stop all if weren't stopped on previous test run StopRedis(false); @@ -31,34 +48,19 @@ public void SetUp() // Get redis version TestConfiguration.REDIS_VERSION = GetRedisVersion(); - TestContext.Progress.WriteLine($"Cluster ports = {string.Join(',', TestConfiguration.CLUSTER_PORTS)}"); - TestContext.Progress.WriteLine($"Standalone ports = {string.Join(',', TestConfiguration.STANDALONE_PORTS)}"); - TestContext.Progress.WriteLine($"Redis version = {TestConfiguration.REDIS_VERSION}"); + TestConsoleWriteLine($"Cluster ports = {string.Join(',', TestConfiguration.CLUSTER_PORTS)}"); + TestConsoleWriteLine($"Standalone ports = {string.Join(',', TestConfiguration.STANDALONE_PORTS)}"); + TestConsoleWriteLine($"Redis version = {TestConfiguration.REDIS_VERSION}"); } - [OneTimeTearDown] - public void TearDown() => + public void Dispose() => // Stop all StopRedis(true); private readonly string _scriptDir; - // Nunit requires a public default constructor. These variables would be set in SetUp method. - public IntegrationTestBase() - { - string? projectDir = Directory.GetCurrentDirectory(); - while (!(Path.GetFileName(projectDir) == "csharp" || projectDir == null)) - { - projectDir = Path.GetDirectoryName(projectDir); - } - - if (projectDir == null) - { - throw new FileNotFoundException("Can't detect the project dir. Are you running tests from `csharp` directory?"); - } - - _scriptDir = Path.Combine(projectDir, "..", "utils"); - } + private void TestConsoleWriteLine(string message) => + _ = _diagnosticMessageSink.OnMessage(new DiagnosticMessage(message)); internal List StartRedis(bool cluster, bool tls = false, string? name = null) { @@ -92,7 +94,7 @@ private string RunClusterManager(string cmd, bool ignoreExitCode) string? output = script?.StandardOutput.ReadToEnd(); int? exit_code = script?.ExitCode; - TestContext.Progress.WriteLine($"cluster_manager.py stdout\n====\n{output}\n====\ncluster_manager.py stderr\n====\n{error}\n====\n"); + TestConsoleWriteLine($"cluster_manager.py stdout\n====\n{output}\n====\ncluster_manager.py stderr\n====\n{error}\n====\n"); return !ignoreExitCode && exit_code != 0 ? throw new ApplicationException($"cluster_manager.py script failed: exit code {exit_code}.") diff --git a/csharp/tests/Usings.cs b/csharp/tests/Usings.cs index 71106ca711..7bb49c0d1f 100644 --- a/csharp/tests/Usings.cs +++ b/csharp/tests/Usings.cs @@ -1,3 +1,3 @@ // Copyright GLIDE-for-Redis Project Contributors - SPDX Identifier: Apache-2.0 -global using NUnit.Framework; +global using Xunit; diff --git a/csharp/tests/tests.csproj b/csharp/tests/tests.csproj index 3d991e1084..dac47a8e06 100644 --- a/csharp/tests/tests.csproj +++ b/csharp/tests/tests.csproj @@ -5,6 +5,8 @@ enable enable preview + false + true true false @@ -16,11 +18,17 @@ - - - - - + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + +