Skip to content

Commit 959cca5

Browse files
authored
feat: include skip message for long running tests (#511)
Add a comment to skipped tests that indicate why they were skipped. ![image](https://github.com/Testably/Testably.Abstractions/assets/3438234/b2f889a5-1c0e-4261-9f0d-599bd2be1275)
1 parent 8547a77 commit 959cca5

37 files changed

+287
-188
lines changed

CONTRIBUTING.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ Additionally each push to the `main` branch checks the quality of the unit tests
1515

1616
## Tests
1717
On the build system, unit tests are executed both against the `MockFileSystem` and the `RealFileSystem`. This ensures that the tests verify correct assumptions.
18-
In order to simplify and speedup the development process, per default some tests are disabled in DEBUG mode.
18+
In order to simplify and speedup the development process, per default, some tests are disabled in DEBUG mode.
1919
These can be enabled by explicitely running the [`Testably.Abstractions.TestSettings`](https://github.com/Testably/Testably.Abstractions/tree/main/Tests/Settings/Testably.Abstractions.TestSettings) tests:
20-
- `LongRunningTestsAlsoInDebugMode` (`Include`/`Exclude`)
21-
In order to increase the test execution speed during development, long-running tests are disabled per default in DEBUG mode. With this setting, the corresponding tests can be included.
22-
- `RealFileSystemTestsInDebugMode` (`Enable`/`Disable`)
23-
If enabled, the classes for executing tests against the real file system also run in DEBUG mode.
20+
- `LongRunningTests` (`AlwaysEnabled` / `DisabledInDebugMode` (default) / `AlwaysDisabled`)
21+
Some tests take a long time to run against the real file system (e.g. timeout). Per default, they are disabled in DEBUG mode.
22+
- `RealFileSystemTests` (`AlwaysEnabled` / `DisabledInDebugMode` (default) / `AlwaysDisabled`)
23+
All tests against the real file system. Per default, they are disabled in DEBUG mode.
2424

2525
*Note: These settings are stored locally in `test.settings.json` which is excluded in [`.gitignore`](https://github.com/Testably/Testably.Abstractions/blob/main/.gitignore) so that it only affects the individual developer!*
2626

Tests/Helpers/Testably.Abstractions.TestHelpers/FileSystemTestBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,5 @@ protected FileSystemTestBase()
4141
/// <summary>
4242
/// Specifies, if long-running tests should be skipped on the real file system.
4343
/// </summary>
44-
public abstract bool LongRunningTestsShouldBeSkipped();
44+
public abstract void SkipIfLongRunningTestsShouldBeSkipped();
4545
}

Tests/Helpers/Testably.Abstractions.TestHelpers/Settings/RealFileSystemFixture.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ namespace Testably.Abstractions.TestHelpers.Settings;
66

77
public class RealFileSystemFixture
88
{
9-
public bool EnableRealFileSystemTestsInDebugMode { get; }
10-
public bool IncludeLongRunningTestsAlsoInDebugMode { get; }
9+
public TestSettingStatus LongRunningTests { get; }
10+
public TestSettingStatus RealFileSystemTests { get; }
1111

1212
public RealFileSystemFixture()
1313
{
@@ -16,15 +16,15 @@ public RealFileSystemFixture()
1616
string path = Path.GetFullPath(
1717
Path.Combine("..", "..", "..", "..", "test.settings.json"));
1818
string content = File.ReadAllText(path);
19-
TestSettings settings = JsonConvert.DeserializeObject<TestSettings>(content)!;
20-
EnableRealFileSystemTestsInDebugMode = settings.EnableRealFileSystemTestsInDebugMode;
21-
IncludeLongRunningTestsAlsoInDebugMode =
22-
settings.IncludeLongRunningTestsAlsoInDebugMode;
19+
TestEnvironment environment = JsonConvert.DeserializeObject<TestEnvironment>(content)!;
20+
21+
RealFileSystemTests = environment.RealFileSystemTests;
22+
LongRunningTests = environment.LongRunningTests;
2323
}
2424
catch (Exception)
2525
{
26-
EnableRealFileSystemTestsInDebugMode = false;
27-
IncludeLongRunningTestsAlsoInDebugMode = false;
26+
RealFileSystemTests = TestSettingStatus.DisabledInDebugMode;
27+
LongRunningTests = TestSettingStatus.DisabledInDebugMode;
2828
}
2929
}
3030
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace Testably.Abstractions.TestHelpers.Settings;
2+
3+
public class TestEnvironment
4+
{
5+
/// <summary>
6+
/// Affects some tests, that take a long time to run against the real file system (e.g. timeout).
7+
/// </summary>
8+
/// <remarks>Per default, they are <see cref="TestSettingStatus.DisabledInDebugMode" />.</remarks>
9+
public TestSettingStatus LongRunningTests { get; set; }
10+
= TestSettingStatus.DisabledInDebugMode;
11+
12+
/// <summary>
13+
/// Affects all tests against the real file system.
14+
/// </summary>
15+
/// <remarks>Per default, they are <see cref="TestSettingStatus.DisabledInDebugMode" />.</remarks>
16+
public TestSettingStatus RealFileSystemTests { get; set; }
17+
= TestSettingStatus.DisabledInDebugMode;
18+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
namespace Testably.Abstractions.TestHelpers.Settings;
2+
3+
public enum TestSettingStatus
4+
{
5+
/// <summary>
6+
/// The tests are always enabled.
7+
/// </summary>
8+
AlwaysEnabled,
9+
10+
/// <summary>
11+
/// The tests are only disabled in DEBUG mode.
12+
/// </summary>
13+
DisabledInDebugMode,
14+
15+
/// <summary>
16+
/// The tests are always disabled.
17+
/// </summary>
18+
AlwaysDisabled
19+
}

Tests/Helpers/Testably.Abstractions.TestHelpers/Settings/TestSettings.cs

Lines changed: 0 additions & 7 deletions
This file was deleted.

Tests/Helpers/Testably.Abstractions.Tests.SourceGenerator/ClassGenerators/FileSystemClassGenerator.cs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ public void Dispose()
5757
=> _directoryCleaner.Dispose();
5858
5959
/// <inheritdoc cref=""{@class.Name}{{TFileSystem}}.LongRunningTestsShouldBeSkipped()"" />
60-
public override bool LongRunningTestsShouldBeSkipped() => false;
60+
public override void SkipIfLongRunningTestsShouldBeSkipped()
61+
{{
62+
// Long-running tests are never skipped against the mock file system!
63+
}}
6164
}}
6265
}}
6366
@@ -77,9 +80,14 @@ public RealFileSystemTests(ITestOutputHelper testOutputHelper, RealFileSystemFix
7780
: base(new Test(), new RealFileSystem(), new RealTimeSystem())
7881
{{
7982
#if DEBUG
80-
if (!fixture.EnableRealFileSystemTestsInDebugMode)
83+
if (fixture.RealFileSystemTests != TestSettingStatus.AlwaysEnabled)
84+
{{
85+
throw new SkipException($""RealFileSystemTests are {{fixture.RealFileSystemTests}}. You can enable them by executing the corresponding tests in Testably.Abstractions.TestSettings.RealFileSystemTests."");
86+
}}
87+
#else
88+
if (fixture.RealFileSystemTests == TestSettingStatus.AlwaysDisabled)
8189
{{
82-
throw new SkipException(""EnableRealFileSystemTestsInDebugMode is not set in test.settings.json"");
90+
throw new SkipException($""RealFileSystemTests are {{fixture.RealFileSystemTests}}. You can enable them by executing the corresponding tests in Testably.Abstractions.TestSettings.RealFileSystemTests."");
8391
}}
8492
#endif
8593
_fixture = fixture;
@@ -91,8 +99,17 @@ public RealFileSystemTests(ITestOutputHelper testOutputHelper, RealFileSystemFix
9199
public void Dispose()
92100
=> _directoryCleaner.Dispose();
93101
102+
#if DEBUG
103+
/// <inheritdoc cref=""{@class.Name}{{TFileSystem}}.LongRunningTestsShouldBeSkipped()"" />
104+
public override void SkipIfLongRunningTestsShouldBeSkipped()
105+
=> Skip.If(_fixture.LongRunningTests != TestSettingStatus.AlwaysEnabled,
106+
$""LongRunningTests are {{_fixture.LongRunningTests}}. You can enable them by executing the corresponding tests in Testably.Abstractions.TestSettings.LongRunningTests."");
107+
#else
94108
/// <inheritdoc cref=""{@class.Name}{{TFileSystem}}.LongRunningTestsShouldBeSkipped()"" />
95-
public override bool LongRunningTestsShouldBeSkipped() => !_fixture.IncludeLongRunningTestsAlsoInDebugMode;
109+
public override void SkipIfLongRunningTestsShouldBeSkipped()
110+
=> Skip.If(_fixture.LongRunningTests == TestSettingStatus.AlwaysDisabled,
111+
$""LongRunningTests are {{_fixture.LongRunningTests}}. You can enable them by executing the corresponding tests in Testably.Abstractions.TestSettings.LongRunningTests."");
112+
#endif
96113
}}
97114
}}");
98115
}

Tests/Settings/Testably.Abstractions.TestSettings/Helper.cs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,49 @@
11
using System;
22
using System.IO;
33
using System.Text.Json;
4+
using System.Text.Json.Serialization;
5+
using Testably.Abstractions.TestHelpers.Settings;
46

57
namespace Testably.Abstractions.TestSettings;
68

79
internal static class Helper
810
{
9-
public static TestHelpers.Settings.TestSettings ChangeTestSettings(
10-
Action<TestHelpers.Settings.TestSettings> change)
11+
public static TestEnvironment ChangeTestSettings(
12+
Action<TestEnvironment> change)
1113
{
12-
TestHelpers.Settings.TestSettings settings = ReadTestSettings();
13-
change(settings);
14-
WriteTestSettings(settings);
15-
return settings;
14+
TestEnvironment environment = ReadTestSettings();
15+
change(environment);
16+
WriteTestSettings(environment);
17+
return environment;
1618
}
1719

1820
private static string GetTestSettingsPath() =>
1921
Path.GetFullPath(Path.Combine("..", "..", "..", "..", "test.settings.json"));
2022

21-
private static TestHelpers.Settings.TestSettings ReadTestSettings()
23+
private static TestEnvironment ReadTestSettings()
2224
{
2325
try
2426
{
2527
string path = GetTestSettingsPath();
2628
string content = File.ReadAllText(path);
27-
return JsonSerializer.Deserialize<TestHelpers.Settings.TestSettings>(content)
29+
return JsonSerializer.Deserialize<TestEnvironment>(content)
2830
?? throw new NotSupportedException("The file has an invalid syntax!");
2931
}
3032
catch (Exception)
3133
{
32-
return new TestHelpers.Settings.TestSettings();
34+
return new TestEnvironment();
3335
}
3436
}
3537

36-
private static void WriteTestSettings(TestHelpers.Settings.TestSettings settings)
38+
private static void WriteTestSettings(TestEnvironment environment)
3739
{
38-
string content = JsonSerializer.Serialize(settings, new JsonSerializerOptions
40+
string content = JsonSerializer.Serialize(environment, new JsonSerializerOptions
3941
{
40-
WriteIndented = true
42+
WriteIndented = true,
43+
Converters =
44+
{
45+
new JsonStringEnumConverter()
46+
}
4147
});
4248
string path = GetTestSettingsPath();
4349
File.WriteAllText(path, content);
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
using NUnit.Framework;
2+
using Testably.Abstractions.TestHelpers.Settings;
3+
4+
namespace Testably.Abstractions.TestSettings;
5+
6+
public sealed class LongRunningTests
7+
{
8+
/// <summary>
9+
/// Some tests take a long time to run against the real file system (e.g. timeout).
10+
/// <para />
11+
/// Always disable these tests!
12+
/// </summary>
13+
[TestCase]
14+
[Explicit]
15+
public void DisableAlways()
16+
{
17+
_ = Helper.ChangeTestSettings(s =>
18+
s.LongRunningTests = TestSettingStatus.AlwaysDisabled);
19+
20+
Assert.Pass("Long-running tests are always disabled.");
21+
}
22+
23+
/// <summary>
24+
/// Some tests take a long time to run against the real file system (e.g. timeout).
25+
/// <para />
26+
/// Disable these tests in DEBUG mode!
27+
/// </summary>
28+
[TestCase]
29+
[Explicit]
30+
public void DisableInDebugMode()
31+
{
32+
TestEnvironment result = Helper.ChangeTestSettings(s =>
33+
s.LongRunningTests = TestSettingStatus.DisabledInDebugMode);
34+
35+
if (result.RealFileSystemTests == TestSettingStatus.AlwaysDisabled)
36+
{
37+
Assert.Warn("""
38+
Long-running tests are always enabled.
39+
But the tests against the real file system are always disabled.
40+
""");
41+
}
42+
else
43+
{
44+
Assert.Pass("Long-running tests are disabled in DEBUG mode.");
45+
}
46+
}
47+
48+
/// <summary>
49+
/// Some tests take a long time to run against the real file system (e.g. timeout).
50+
/// <para />
51+
/// Always enable these tests!
52+
/// </summary>
53+
[TestCase]
54+
[Explicit]
55+
public void EnableAlways()
56+
{
57+
TestEnvironment result = Helper.ChangeTestSettings(s =>
58+
s.LongRunningTests = TestSettingStatus.AlwaysEnabled);
59+
60+
if (result.RealFileSystemTests != TestSettingStatus.AlwaysEnabled)
61+
{
62+
Assert.Warn("""
63+
Long-running tests are always enabled.
64+
But the tests against the real file system are not always enabled.
65+
""");
66+
}
67+
else
68+
{
69+
Assert.Pass("Long-running tests are always enabled.");
70+
}
71+
}
72+
73+
[TestCase]
74+
public void TestDummy_SoThatExplicitTestsAreIgnored()
75+
{
76+
Assert.Pass();
77+
}
78+
}

Tests/Settings/Testably.Abstractions.TestSettings/LongRunningTestsAlsoInDebugMode.cs

Lines changed: 0 additions & 52 deletions
This file was deleted.

0 commit comments

Comments
 (0)