Skip to content

Commit 9d57b9b

Browse files
authored
docs: Added XML docs for the first batch of TUnit Attributes (#2468)
* docs: Added XML docs for the first batch of TUnit Attributes * fix: Wrongly attached interface removed
1 parent 81213ac commit 9d57b9b

11 files changed

+513
-3
lines changed

TUnit.Core/Attributes/TestMetadata/CategoryAttribute.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,62 @@
22

33
namespace TUnit.Core;
44

5+
/// <summary>
6+
/// Adds a category to a test method, test class, or assembly for organizational and filtering purposes.
7+
/// </summary>
8+
/// <remarks>
9+
/// <para>
10+
/// Categories provide a way to group tests for better organization and selective execution.
11+
/// Tests can be filtered by category when running tests through the TUnit test runner.
12+
/// </para>
13+
///
14+
/// <para>
15+
/// The attribute can be applied at different levels:
16+
/// </para>
17+
/// <list type="bullet">
18+
/// <item>Method level: Categorizes a specific test method</item>
19+
/// <item>Class level: Categorizes all test methods in the class</item>
20+
/// <item>Assembly level: Categorizes all test methods in the assembly</item>
21+
/// </list>
22+
///
23+
/// <para>
24+
/// Multiple categories can be applied to the same target by using multiple instances of this attribute.
25+
/// </para>
26+
/// </remarks>
27+
/// <example>
28+
/// <code>
29+
/// [Category("Integration")]
30+
/// public class IntegrationTests
31+
/// {
32+
/// [Test]
33+
/// [Category("Database")]
34+
/// public void DatabaseTest()
35+
/// {
36+
/// // Test implementation
37+
/// }
38+
///
39+
/// [Test]
40+
/// [Category("API")]
41+
/// [Category("Authentication")]
42+
/// public void ApiAuthenticationTest()
43+
/// {
44+
/// // Test implementation
45+
/// }
46+
/// }
47+
/// </code>
48+
/// </example>
549
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Assembly, AllowMultiple = true)]
650
public class CategoryAttribute(string category) : TUnitAttribute, ITestDiscoveryEventReceiver
751
{
52+
/// <inheritdoc />
853
public int Order => 0;
954

55+
/// <summary>
56+
/// Gets the name of the category.
57+
/// </summary>
1058
public string Category { get; } = category;
1159

60+
/// <inheritdoc />
1261
public void OnTestDiscovery(DiscoveredTestContext discoveredTestContext)
1362
{
1463
discoveredTestContext.AddCategory(Category);

TUnit.Core/Attributes/TestMetadata/DependsOnAttribute.cs

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,210 @@
11
namespace TUnit.Core;
22

3+
/// <summary>
4+
/// Generic version of the <see cref="DependsOnAttribute"/> that specifies a dependency on a test method in a specific class.
5+
/// </summary>
6+
/// <typeparam name="T">The type that contains the test method this test depends on.</typeparam>
7+
/// <remarks>
8+
/// This generic version simplifies specifying dependencies with strong typing, avoiding the need to use <see cref="Type"/> parameters directly.
9+
/// </remarks>
10+
/// <example>
11+
/// <code>
12+
/// // The FeatureTest will only run if UserLoginTest passes
13+
/// [Test]
14+
/// [DependsOn&lt;LoginTests&gt;("UserLoginTest")]
15+
/// public void FeatureTest()
16+
/// {
17+
/// // This test will only run if UserLoginTest in the LoginTests class has passed
18+
/// }
19+
/// </code>
20+
/// </example>
321
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = true)]
422
public class DependsOnAttribute<T> : DependsOnAttribute
523
{
24+
/// <summary>
25+
/// Initializes a new instance of the <see cref="DependsOnAttribute{T}"/> class.
26+
/// Specifies a dependency on all test methods in the specified class.
27+
/// </summary>
628
public DependsOnAttribute() : base(typeof(T))
729
{
830
}
931

32+
/// <summary>
33+
/// Initializes a new instance of the <see cref="DependsOnAttribute{T}"/> class.
34+
/// Specifies a dependency on a specific test method in the specified class.
35+
/// </summary>
36+
/// <param name="testName">The name of the test method that must run successfully before this test.</param>
1037
public DependsOnAttribute(string testName) : base(typeof(T), testName)
1138
{
1239
}
1340

41+
/// <summary>
42+
/// Initializes a new instance of the <see cref="DependsOnAttribute{T}"/> class.
43+
/// Specifies a dependency on a specific test method with parameter types in the specified class.
44+
/// </summary>
45+
/// <param name="testName">The name of the test method that must run successfully before this test.</param>
46+
/// <param name="parameterTypes">The parameter types of the test method, used to disambiguate overloaded methods.</param>
1447
public DependsOnAttribute(string testName, Type[] parameterTypes) : base(typeof(T), testName, parameterTypes)
1548
{
1649
}
1750
}
1851

52+
/// <summary>
53+
/// Specifies that a test method or class has dependencies on other test methods that must run successfully before this test can execute.
54+
/// </summary>
55+
/// <remarks>
56+
/// <para>
57+
/// Use this attribute when you have tests that depend on the successful execution of other tests.
58+
/// A test decorated with this attribute will only run if all its dependencies have passed.
59+
/// </para>
60+
///
61+
/// <para>
62+
/// Dependencies can be specified by:
63+
/// </para>
64+
/// <list type="bullet">
65+
/// <item>Test method name (when depending on a test in the same class)</item>
66+
/// <item>Test class type (when depending on all tests in another class)</item>
67+
/// <item>Combination of test class type and method name (when depending on a specific test in another class)</item>
68+
/// <item>Test method name and parameter types (when depending on a specific overloaded method)</item>
69+
/// </list>
70+
///
71+
/// <para>
72+
/// For better type safety, use the generic version <see cref="DependsOnAttribute{T}"/> when specifying dependencies on tests in other classes.
73+
/// </para>
74+
///
75+
/// <para>
76+
/// When <see cref="ProceedOnFailure"/> is set to true, the test will run even if its dependencies failed.
77+
/// By default, a test will be skipped if any of its dependencies failed.
78+
/// </para>
79+
/// </remarks>
80+
/// <example>
81+
/// <code>
82+
/// // Simple dependency on a test in the same class
83+
/// [Test]
84+
/// public void FirstTest() { }
85+
///
86+
/// [Test]
87+
/// [DependsOn("FirstTest")]
88+
/// public void SecondTest()
89+
/// {
90+
/// // This test will only run if FirstTest passes
91+
/// }
92+
///
93+
/// // Dependency on a test in another class
94+
/// [Test]
95+
/// [DependsOn(typeof(SetupTests), "Initialize")]
96+
/// public void TestRequiringSetup()
97+
/// {
98+
/// // This test will only run if the Initialize test in SetupTests passes
99+
/// }
100+
///
101+
/// // Dependency with overloaded method disambiguation
102+
/// [Test]
103+
/// [DependsOn("OverloadedTest", new Type[] { typeof(string), typeof(int) })]
104+
/// public void TestWithOverloadDependency() { }
105+
///
106+
/// // Proceeding even if dependency fails
107+
/// [Test]
108+
/// [DependsOn("CriticalTest") { ProceedOnFailure = true }]
109+
/// public void TestThatRunsAnyway()
110+
/// {
111+
/// // This test will run even if CriticalTest fails
112+
/// }
113+
/// </code>
114+
/// </example>
19115
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = true)]
20116
public class DependsOnAttribute : TUnitAttribute
21117
{
118+
/// <summary>
119+
/// Initializes a new instance of the <see cref="DependsOnAttribute"/> class.
120+
/// Specifies a dependency on a test method in the same class.
121+
/// </summary>
122+
/// <param name="testName">The name of the test method that must run successfully before this test.</param>
22123
public DependsOnAttribute(string testName) : this(testName, null!)
23124
{
24125
}
25126

127+
/// <summary>
128+
/// Initializes a new instance of the <see cref="DependsOnAttribute"/> class.
129+
/// Specifies a dependency on an overloaded test method in the same class.
130+
/// </summary>
131+
/// <param name="testName">The name of the test method that must run successfully before this test.</param>
132+
/// <param name="parameterTypes">The parameter types of the test method, used to disambiguate overloaded methods.</param>
26133
public DependsOnAttribute(string testName, Type[] parameterTypes) : this(null!, testName, parameterTypes)
27134
{
28135
}
29136

137+
/// <summary>
138+
/// Initializes a new instance of the <see cref="DependsOnAttribute"/> class.
139+
/// Specifies a dependency on all test methods in a specific class.
140+
/// </summary>
141+
/// <param name="testClass">The class containing the test methods that must run successfully before this test.</param>
30142
public DependsOnAttribute(Type testClass) : this(testClass, null!)
31143
{
32144
}
33145

146+
/// <summary>
147+
/// Initializes a new instance of the <see cref="DependsOnAttribute"/> class.
148+
/// Specifies a dependency on a specific test method in a specific class.
149+
/// </summary>
150+
/// <param name="testClass">The class containing the test method that must run successfully before this test.</param>
151+
/// <param name="testName">The name of the test method that must run successfully before this test.</param>
34152
public DependsOnAttribute(Type testClass, string testName) : this(testClass, testName, null!)
35153
{
36154
}
37155

156+
/// <summary>
157+
/// Initializes a new instance of the <see cref="DependsOnAttribute"/> class.
158+
/// Specifies a dependency on a specific overloaded test method in a specific class.
159+
/// </summary>
160+
/// <param name="testClass">The class containing the test method that must run successfully before this test.</param>
161+
/// <param name="testName">The name of the test method that must run successfully before this test.</param>
162+
/// <param name="parameterTypes">The parameter types of the test method, used to disambiguate overloaded methods.</param>
38163
public DependsOnAttribute(Type testClass, string testName, Type[] parameterTypes)
39164
{
40165
TestClass = testClass;
41166
TestName = testName;
42167
ParameterTypes = parameterTypes;
43168
}
44169

170+
/// <summary>
171+
/// Gets the class containing the test method this test depends on.
172+
/// </summary>
173+
/// <remarks>
174+
/// If null, the dependency is assumed to be on a test in the same class.
175+
/// </remarks>
45176
public Type? TestClass { get; }
46177

178+
/// <summary>
179+
/// Gets the name of the test method this test depends on.
180+
/// </summary>
181+
/// <remarks>
182+
/// If null, the dependency is assumed to be on all tests in the <see cref="TestClass"/>.
183+
/// </remarks>
47184
public string? TestName { get; }
185+
186+
/// <summary>
187+
/// Gets the parameter types of the test method this test depends on.
188+
/// </summary>
189+
/// <remarks>
190+
/// Used to disambiguate overloaded methods with the same name.
191+
/// If null, the first method matching <see cref="TestName"/> will be used.
192+
/// </remarks>
48193
public Type[]? ParameterTypes { get; }
49194

195+
/// <summary>
196+
/// Gets or sets a value indicating whether this test should proceed even if its dependencies have failed.
197+
/// </summary>
198+
/// <remarks>
199+
/// When set to true, the test will run even if its dependencies failed.
200+
/// When set to false (default), the test will be skipped if any of its dependencies failed.
201+
/// </remarks>
50202
public bool ProceedOnFailure { get; set; }
51203

204+
/// <summary>
205+
/// Returns a string representation of the dependency.
206+
/// </summary>
207+
/// <returns>A string that represents the dependency.</returns>
52208
public override string ToString()
53209
{
54210
if (TestClass != null && TestName == null)

TUnit.Core/Attributes/TestMetadata/DisplayNameAttribute.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,34 @@
44

55
namespace TUnit.Core;
66

7+
/// <summary>
8+
/// Attribute that allows specifying a custom display name for a test method.
9+
/// </summary>
10+
/// <remarks>
11+
/// <para>
12+
/// This attribute can be applied to test methods to provide more descriptive names than the default method name.
13+
/// </para>
14+
/// <para>
15+
/// The display name can include parameter placeholders in the format of "$parameterName" which will be
16+
/// replaced with the actual parameter values during test execution. For example:
17+
/// <code>
18+
/// [Test]
19+
/// [Arguments("John", 25)]
20+
/// [DisplayName("User $name is $age years old")]
21+
/// public void TestUser(string name, int age) { ... }
22+
/// </code>
23+
/// </para>
24+
/// <para>
25+
/// When this test runs, the display name would appear as "User John is 25 years old".
26+
/// </para>
27+
/// </remarks>
28+
/// <param name="displayName">
29+
/// The display name template. Can include parameter placeholders in the format of "$parameterName".
30+
/// </param>
731
[AttributeUsage(AttributeTargets.Method, Inherited = false)]
832
public sealed class DisplayNameAttribute(string displayName) : DisplayNameFormatterAttribute
933
{
34+
/// <inheritdoc />
1035
protected override string FormatDisplayName(TestContext testContext)
1136
{
1237
var testDetails = testContext.TestDetails;

TUnit.Core/Attributes/TestMetadata/DisplayNameFormatterAttribute.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,37 @@
44

55
namespace TUnit.Core;
66

7+
/// <summary>
8+
/// Base attribute class that provides functionality for formatting display names of tests in TUnit.
9+
/// Concrete implementations of this class can customize how test names are displayed in test results.
10+
/// </summary>
11+
/// <remarks>
12+
/// This attribute can be applied at the method, class, or assembly level to control test name formatting.
13+
/// Subclasses must implement the <see cref="FormatDisplayName"/> method to define custom formatting logic.
14+
/// The attribute interfaces with the test discovery system to set display names for tests during discovery.
15+
/// </remarks>
716
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Assembly, Inherited = false)]
817
public abstract class DisplayNameFormatterAttribute : TUnitAttribute, ITestDiscoveryEventReceiver
918
{
19+
/// <inheritdoc />
1020
public int Order => 0;
1121

22+
/// <inheritdoc />
1223
public void OnTestDiscovery(DiscoveredTestContext discoveredTestContext)
1324
{
1425
var displayName = FormatDisplayName(discoveredTestContext.TestContext);
1526

1627
discoveredTestContext.SetDisplayName(displayName);
1728
}
1829

30+
/// <summary>
31+
/// When implemented in derived classes, formats the display name for a test.
32+
/// </summary>
33+
/// <param name="testContext">
34+
/// The test context containing information about the test being discovered.
35+
/// </param>
36+
/// <returns>
37+
/// A string containing the formatted display name for the test.
38+
/// </returns>
1939
protected abstract string FormatDisplayName(TestContext testContext);
2040
}

TUnit.Core/Attributes/TestMetadata/PropertyAttribute.cs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,46 @@
22

33
namespace TUnit.Core;
44

5+
/// <summary>
6+
/// Attaches a custom property with a name-value pair to a test method, test class, or assembly.
7+
/// These properties can be used for filtering tests or for providing additional metadata.
8+
/// </summary>
9+
/// <remarks>
10+
/// Properties added with this attribute are available during test discovery and execution.
11+
/// Multiple properties can be added to the same target by using multiple instances of this attribute.
12+
/// </remarks>
13+
/// <example>
14+
/// <code>
15+
/// [Property("Category", "Integration")]
16+
/// [Property("Owner", "TestTeam")]
17+
/// public class MyIntegrationTests
18+
/// {
19+
/// [Test]
20+
/// [Property("Priority", "High")]
21+
/// public void HighPriorityTest()
22+
/// {
23+
/// // Test implementation
24+
/// }
25+
/// }
26+
/// </code>
27+
/// </example>
528
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Assembly, AllowMultiple = true)]
629
public class PropertyAttribute(string name, string value) : TUnitAttribute, ITestDiscoveryEventReceiver
730
{
31+
/// <inheritdoc />
832
public int Order => 0;
933

34+
/// <summary>
35+
/// Gets the name of the property.
36+
/// </summary>
1037
public string Name { get; } = name;
11-
public string Value { get; } = value;
1238

39+
/// <summary>
40+
/// Gets the value of the property.
41+
/// </summary>
42+
public string Value { get; } = value;
43+
44+
/// <inheritdoc />
1345
public void OnTestDiscovery(DiscoveredTestContext discoveredTestContext)
1446
{
1547
discoveredTestContext.AddProperty(Name, Value);

0 commit comments

Comments
 (0)