Replies: 3 comments 4 replies
-
|
One of the main reasons for this is that the Consider this:
If assertions executed immediately, without the await keyword, this may fail and throw immediately on the Sure, I could have opted for an I can't really think of a way around this with the current state of the language. I think your options are:
|
Beta Was this translation helpful? Give feedback.
-
|
This isn't about chaining assertion calls, it's about valid C# coding scenarios. In the unsafe example, this code now fails: var pValue = &value;This isn't even in an assertion, but because the whole test now needs to be With a await Assert.That(data[0]).IsEqualTo(1);That isn't chaining, just a simple equality check. But because I have to |
Beta Was this translation helpful? Give feedback.
-
|
Would it make sense to keep the tasks and process them all at once with #pragma warning disable TUnitAssertions0002
[Test]
public async Task SpanTest()
{
Span<int> span = [1, 2, 3];
var list = new List<Task>();
using (Assert.Multiple())
{
list.Add(Assert.That(span.Length).IsNotEqualTo(3).AssertAsync());
list.Add(Assert.That(span[^1]).IsNotEqualTo(3).AssertAsync());
}
await Task.WhenAll(list);
}
[Test]
public async Task SpanTestWithUsing()
{
Span<int> span = [1, 2, 3];
await using (var multi = new MultipleAsync())
{
multi.Add(Assert.That(span.Length).IsNotEqualTo(3));
multi.Add(Assert.That(span[^1]).IsNotEqualTo(3));
}
}
class MultipleAsync : IAsyncDisposable
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "TUnitAssertions0004")]
IDisposable AssertionScope = Assert.Multiple();
List<Task> list = new();
public void Add(IAssertion assertion)
{
list.Add(assertion.AssertAsync());
}
public async ValueTask DisposeAsync()
{
AssertionScope.Dispose();
await Task.WhenAll(list);
}
}
#pragma warning restore TUnitAssertions0002 |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Overall, I like TUnit, but one area I'm finding problematic is with its insistence that everything must be asynchronous. This is starting to become kind of a PITA in certain scenarios:
async, you start to run into issues like CS4004 and CS9123.Span<>usage. If I have aSpan<>in a test, and I want to read values from thatSpan<>withinAssert.That(), I can't, because I'll getCS4007. I can get around this by capturing all the values before I start doing assertions, but this is kind of clumsy and unnecessary.While unsafe code isn't common, it is valid. Furthermore,
Span<>types are showing up more often, and eliminating theasyncboundary would make tests easier to write. I feel like there should be some consideration to having synchronous assertions. Curious to hear what others have to say.Beta Was this translation helpful? Give feedback.
All reactions