|
1 | 1 | namespace TUnit.Core; |
2 | 2 |
|
| 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<LoginTests>("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> |
3 | 21 | [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = true)] |
4 | 22 | public class DependsOnAttribute<T> : DependsOnAttribute |
5 | 23 | { |
| 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> |
6 | 28 | public DependsOnAttribute() : base(typeof(T)) |
7 | 29 | { |
8 | 30 | } |
9 | 31 |
|
| 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> |
10 | 37 | public DependsOnAttribute(string testName) : base(typeof(T), testName) |
11 | 38 | { |
12 | 39 | } |
13 | 40 |
|
| 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> |
14 | 47 | public DependsOnAttribute(string testName, Type[] parameterTypes) : base(typeof(T), testName, parameterTypes) |
15 | 48 | { |
16 | 49 | } |
17 | 50 | } |
18 | 51 |
|
| 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> |
19 | 115 | [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = true)] |
20 | 116 | public class DependsOnAttribute : TUnitAttribute |
21 | 117 | { |
| 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> |
22 | 123 | public DependsOnAttribute(string testName) : this(testName, null!) |
23 | 124 | { |
24 | 125 | } |
25 | 126 |
|
| 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> |
26 | 133 | public DependsOnAttribute(string testName, Type[] parameterTypes) : this(null!, testName, parameterTypes) |
27 | 134 | { |
28 | 135 | } |
29 | 136 |
|
| 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> |
30 | 142 | public DependsOnAttribute(Type testClass) : this(testClass, null!) |
31 | 143 | { |
32 | 144 | } |
33 | 145 |
|
| 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> |
34 | 152 | public DependsOnAttribute(Type testClass, string testName) : this(testClass, testName, null!) |
35 | 153 | { |
36 | 154 | } |
37 | 155 |
|
| 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> |
38 | 163 | public DependsOnAttribute(Type testClass, string testName, Type[] parameterTypes) |
39 | 164 | { |
40 | 165 | TestClass = testClass; |
41 | 166 | TestName = testName; |
42 | 167 | ParameterTypes = parameterTypes; |
43 | 168 | } |
44 | 169 |
|
| 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> |
45 | 176 | public Type? TestClass { get; } |
46 | 177 |
|
| 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> |
47 | 184 | 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> |
48 | 193 | public Type[]? ParameterTypes { get; } |
49 | 194 |
|
| 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> |
50 | 202 | public bool ProceedOnFailure { get; set; } |
51 | 203 |
|
| 204 | + /// <summary> |
| 205 | + /// Returns a string representation of the dependency. |
| 206 | + /// </summary> |
| 207 | + /// <returns>A string that represents the dependency.</returns> |
52 | 208 | public override string ToString() |
53 | 209 | { |
54 | 210 | if (TestClass != null && TestName == null) |
|
0 commit comments