Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,22 @@ namespace IntelliTect.TestTools.TestFramework.Tests.TestCaseTests
{
public class FinallyExecutionTests
{
[Fact]
public async Task NoExceptionsWhenAllBlocksAndFinallyBlocksPass()
{
// Arrange
TestCase tc = new TestBuilder()
.AddDependencyInstance(true)
.AddTestBlock<ExampleTestBlockWithBoolReturn>()
.AddFinallyBlock<ExampleFinallyBlock>(true)
.Build();

// Act
await tc.ExecuteAsync();

// Assert
Assert.True(tc.Passed, "Test case did not get marked as Passed when we expected it.");
}

[Fact]
public async Task FinallyBlockThrowsExpectedExceptionWhenNotOverridingDefaultFinallyBehavior()
Expand Down Expand Up @@ -88,5 +104,102 @@ public async Task OnlyTestBlockThrowsExpectedExceptionWhenOverridingDefaultFinal
// Assert
Assert.False(tc.Passed, "Test case did not get marked as Failed when we expected it.");
}

[Fact]
public async Task NoExceptionsWhenAllBlocksAndAsyncFinallyBlocksPass()
{
// Arrange
TestCase tc = new TestBuilder()
.AddDependencyInstance(true)
.AddTestBlock<ExampleTestBlockWithBoolReturn>()
.AddAsyncFinallyBlock<ExampleAsyncFinallyBlock>(true)
.Build();

// Act
await tc.ExecuteAsync();

// Assert
Assert.True(tc.Passed, "Test case did not get marked as Passed when we expected it.");
}

[Fact]
public async Task AsyncFinallyBlockThrowsExpectedExceptionWhenNotOverridingDefaultFinallyBehavior()
{
// Arrange
TestCase tc = new TestBuilder()
.AddDependencyInstance(true)
.AddTestBlock<ExampleTestBlockWithBoolReturn>()
.AddAsyncFinallyBlock<ExampleAsyncFinallyBlock>()
.Build();

// Act
var ex = await Assert.ThrowsAsync<AggregateException>(() => tc.ExecuteAsync());

// Assert
Assert.NotNull(ex.InnerExceptions);
Assert.Single(ex.InnerExceptions);
Assert.Contains("Test case succeeded",
ex.Message,
StringComparison.InvariantCultureIgnoreCase);
Assert.True(tc.Passed, "Test case did not get marked as Passed when we expected it.");
}

[Fact]
public async Task TestBlockAndAsyncFinallyBlockThrowsExpectedExceptionWhenNotOverridingDefaultFinallyBehavior()
{
// Arrange
TestCase tc = new TestBuilder()
.AddDependencyInstance(false)
.AddTestBlock<ExampleTestBlockWithBoolReturn>()
.AddAsyncFinallyBlock<ExampleAsyncFinallyBlock>()
.Build();

// Act
var ex = await Assert.ThrowsAsync<AggregateException>(() => tc.ExecuteAsync());

// Assert
Assert.NotNull(ex.InnerExceptions);
Assert.Equal(2, ex.InnerExceptions.Count);
Assert.Contains("Test case failed and finally blocks failed",
ex.Message,
StringComparison.InvariantCultureIgnoreCase);
Assert.False(tc.Passed, "Test case did not get marked as Failed when we expected it.");
}

[Fact]
public async Task AsyncFinallyBlockDoesNotThrowExceptionWhenOverridingDefaultFinallyBehavior()
{
// Arrange
TestCase tc = new TestBuilder()
.AddDependencyInstance(true)
.AddTestBlock<ExampleTestBlockWithBoolReturn>()
.AddAsyncFinallyBlock<ExampleAsyncFinallyBlock>()
.Build();
tc.ThrowOnFinallyBlockException = false;

// Act
await tc.ExecuteAsync();

// Assert
Assert.True(tc.Passed, "Test case did not get marked as Passed when we expected it.");
}

[Fact]
public async Task OnlyTestBlockThrowsExpectedExceptionWhenOverridingDefaultFinallyBehaviorWithAsyncFinallyBlock()
{
// Arrange
TestCase tc = new TestBuilder()
.AddDependencyInstance(false)
.AddTestBlock<ExampleTestBlockWithBoolReturn>()
.AddAsyncFinallyBlock<ExampleAsyncFinallyBlock>()
.Build();
tc.ThrowOnFinallyBlockException = false;

// Act
await Assert.ThrowsAsync<TestCaseException>(() => tc.ExecuteAsync());

// Assert
Assert.False(tc.Passed, "Test case did not get marked as Failed when we expected it.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,15 @@ public void Execute(bool result)
}
}

public class ExampleAsyncFinallyBlock : TestBlock
{
public async Task Execute(bool result)
{
await Task.Delay(1);
Assert.True(result, "This is an expected failure.");
}
}

public class ExampleAsyncBlockWithReturn : TestBlock
{
public async Task<bool> Execute()
Expand Down
2 changes: 1 addition & 1 deletion IntelliTect.TestTools.TestFramework/TestBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public TestBuilder AddAsyncFinallyBlock<T>(params object[] testBlockArgs) where
Block fb = CreateBlock<T>(testBlockArgs);
fb.IsFinallyBlock = true;
fb.IsAsync = true;
TestBlocks.Add(fb);
FinallyBlocks.Add(fb);
return this;
}

Expand Down
Loading