Skip to content

Commit 0360686

Browse files
authored
fix: resolve GetTestFixture method reliability issues (#566)
- Replace unreliable IsSuite check with direct TestFixture type validation - Add comprehensive error handling with detailed exception messages - Improve code formatting consistency across the helper class - Handle edge cases in test hierarchy traversal
1 parent bc18989 commit 0360686

File tree

1 file changed

+29
-27
lines changed

1 file changed

+29
-27
lines changed

Allure.NUnit/Core/AllureNUnitHelper.cs

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,7 @@ internal void StopTestCase()
7676
: null;
7777

7878

79-
AllureLifecycle.StopTestCase(
80-
testCase =>
79+
AllureLifecycle.StopTestCase(testCase =>
8180
{
8281
testCase.status = status;
8382
testCase.statusDetails = statusDetails;
@@ -147,9 +146,8 @@ TestResultContainer CreateTestContainer() =>
147146

148147
static bool IsBroken(TestContext.ResultAdapter result) =>
149148
!result.Assertions.Any()
150-
|| result.Assertions.Any(
151-
a => a.Status == AssertionStatus.Error
152-
);
149+
|| result.Assertions.Any(a => a.Status == AssertionStatus.Error
150+
);
153151

154152
static void SetIdentifiers(ITest test, TestResult testResult)
155153
{
@@ -217,57 +215,61 @@ static IEnumerable<AllureTestCaseAttribute> IterateAllAllureAttribites(ITest tes
217215
static string GetNamespace(string classFullName)
218216
{
219217
var lastDotIndex = classFullName?.LastIndexOf('.') ?? -1;
220-
return lastDotIndex == -1 ? null : classFullName.Substring(
221-
0,
222-
lastDotIndex
223-
);
218+
return lastDotIndex == -1
219+
? null
220+
: classFullName.Substring(
221+
0,
222+
lastDotIndex
223+
);
224224
}
225225

226226
static string GetClassName(string classFullName)
227227
{
228228
var lastDotIndex = classFullName?.LastIndexOf('.') ?? -1;
229-
return lastDotIndex == -1 ? classFullName : classFullName.Substring(
230-
lastDotIndex + 1
231-
);
229+
return lastDotIndex == -1
230+
? classFullName
231+
: classFullName.Substring(
232+
lastDotIndex + 1
233+
);
232234
}
233235

234236
static TestFixture GetTestFixture(ITest test)
235237
{
236238
var currentTest = test;
237-
var isTestSuite = currentTest.IsSuite;
238-
while (isTestSuite != true)
239+
240+
while (currentTest != null)
239241
{
240-
currentTest = currentTest.Parent;
241-
if (currentTest is ParameterizedMethodSuite)
242+
if (currentTest is TestFixture testFixture)
242243
{
243-
currentTest = currentTest.Parent;
244+
return testFixture;
244245
}
245-
isTestSuite = currentTest.IsSuite;
246+
247+
currentTest = currentTest.Parent;
246248
}
247-
248-
return (TestFixture) currentTest;
249+
250+
throw new InvalidOperationException(
251+
$"Could not find TestFixture in the hierarchy for test: {test.FullName}. " +
252+
$"Test type: {test.GetType().Name}"
253+
);
249254
}
250255

251256
private void UpdateTestDataFromNUnitProperties()
252257
{
253258
foreach (var p in GetTestProperties(PropertyNames.Description))
254259
{
255-
AllureLifecycle.UpdateTestCase(
256-
x => x.description += $"{p}\n"
260+
AllureLifecycle.UpdateTestCase(x => x.description += $"{p}\n"
257261
);
258262
}
259263

260264
foreach (var p in GetTestProperties(PropertyNames.Author))
261265
{
262-
AllureLifecycle.UpdateTestCase(
263-
x => x.labels.Add(Label.Owner(p))
266+
AllureLifecycle.UpdateTestCase(x => x.labels.Add(Label.Owner(p))
264267
);
265268
}
266269

267270
foreach (var p in GetTestProperties(PropertyNames.Category))
268271
{
269-
AllureLifecycle.UpdateTestCase(
270-
x => x.labels.Add(Label.Tag(p))
272+
AllureLifecycle.UpdateTestCase(x => x.labels.Add(Label.Tag(p))
271273
);
272274
}
273275
}
@@ -294,7 +296,7 @@ private IEnumerable<string> GetTestProperties(string name)
294296
var list = new List<string>();
295297
var currentTest = _test;
296298
while (currentTest.GetType() != typeof(TestSuite)
297-
&& currentTest.GetType() != typeof(TestAssembly))
299+
&& currentTest.GetType() != typeof(TestAssembly))
298300
{
299301
if (currentTest.Properties.ContainsKey(name))
300302
{

0 commit comments

Comments
 (0)