Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 36 additions & 16 deletions TUnit.Core/TestContext.Dependencies.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,23 @@ internal List<TestContext> GetTests(Func<TestContext, bool> predicate)
return [];
}

var tests = testFinder.GetTests(classType).Where(predicate).ToList();
var allTests = testFinder.GetTests(classType);
var tests = new List<TestContext>();
var hasUnfinished = false;

if (tests.Any(x => x.Result == null))
foreach (var test in allTests)
{
if (predicate(test))
{
tests.Add(test);
if (test.Result == null)
{
hasUnfinished = true;
}
}
}

if (hasUnfinished)
{
throw new InvalidOperationException(
"Cannot get unfinished tests - Did you mean to add a [DependsOn] attribute?"
Expand All @@ -47,43 +61,49 @@ internal List<TestContext> GetTests(string testName)

var classType = TestDetails.ClassType;

var tests = testFinder.GetTestsByNameAndParameters(
var testsArray = testFinder.GetTestsByNameAndParameters(
testName,
[],
classType,
[],
[]
).ToList();
);

if (tests.Any(x => x.Result == null))
foreach (var test in testsArray)
{
throw new InvalidOperationException(
"Cannot get unfinished tests - Did you mean to add a [DependsOn] attribute?"
);
if (test.Result == null)
{
throw new InvalidOperationException(
"Cannot get unfinished tests - Did you mean to add a [DependsOn] attribute?"
);
}
}

return tests;
return new List<TestContext>(testsArray);
}

internal List<TestContext> GetTests(string testName, Type classType)
{
var testFinder = ServiceProvider.GetService<ITestFinder>()!;

var tests = testFinder.GetTestsByNameAndParameters(
var testsArray = testFinder.GetTestsByNameAndParameters(
testName,
[],
classType,
[],
[]
).ToList();
);

if (tests.Any(x => x.Result == null))
foreach (var test in testsArray)
{
throw new InvalidOperationException(
"Cannot get unfinished tests - Did you mean to add a [DependsOn] attribute?"
);
if (test.Result == null)
{
throw new InvalidOperationException(
"Cannot get unfinished tests - Did you mean to add a [DependsOn] attribute?"
);
}
}

return tests;
return new List<TestContext>(testsArray);
}
}
4 changes: 2 additions & 2 deletions TUnit.Engine/Services/ObjectLifecycleService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -440,8 +440,8 @@ private async Task InitializeNestedObjectsAsync(

var objectsAtDepth = graph.GetObjectsAtDepth(depth);

// Pre-allocate task list without LINQ Select
var tasks = new List<Task>();
// Pre-allocate task list with known capacity
var tasks = new List<Task>(objectsAtDepth.Count);
foreach (var obj in objectsAtDepth)
{
tasks.Add(initializer(obj, cancellationToken).AsTask());
Expand Down
27 changes: 24 additions & 3 deletions TUnit.Engine/Services/TestDependencyResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,14 @@ private bool ResolveDependenciesForTest(AbstractExecutableTest test)
}
}

test.Dependencies = uniqueDependencies.Values.ToArray();
var depsArray = new ResolvedDependency[uniqueDependencies.Count];
var i = 0;
foreach (var dep in uniqueDependencies.Values)
{
depsArray[i++] = dep;
}

test.Dependencies = depsArray;

_testsWithPendingDependencies.Remove(test);

Expand Down Expand Up @@ -191,7 +198,14 @@ private void ResolvePendingDependencies()
/// </summary>
public void BatchResolveDependencies(List<AbstractExecutableTest> tests)
{
var testsWithDependencies = tests.Where(t => t.Metadata.Dependencies.Length > 0).ToList();
var testsWithDependencies = new List<AbstractExecutableTest>();
foreach (var t in tests)
{
if (t.Metadata.Dependencies.Length > 0)
{
testsWithDependencies.Add(t);
}
}

if (testsWithDependencies.Count == 0)
{
Expand Down Expand Up @@ -256,7 +270,14 @@ private void ResolveDependenciesForTestLockFree(AbstractExecutableTest test)
}
}

test.Dependencies = uniqueDependencies.Values.ToArray();
var depsArray = new ResolvedDependency[uniqueDependencies.Count];
var i = 0;
foreach (var dep in uniqueDependencies.Values)
{
depsArray[i++] = dep;
}

test.Dependencies = depsArray;
}
}

Expand Down
Loading