Skip to content

Commit

Permalink
Call Dispose after Xunit test where test class inherits IDisposable (#…
Browse files Browse the repository at this point in the history
…519)

Call Dispose after Xunit test where test class inherits IDisposable
  • Loading branch information
LaurenceJKing authored Jun 6, 2020
1 parent 6f05ef2 commit ad7124f
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 5 deletions.
4 changes: 2 additions & 2 deletions global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"sdk": {
"sdk": {
"version": "3.1.101",
"rollForward": "latestMajor"
}
}
}
14 changes: 12 additions & 2 deletions src/FsCheck.Xunit/PropertyAttribute.fs
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,16 @@ type PropertyTestCase(diagnosticMessageSink:IMessageSink, defaultMethodDisplay:T
let summary = new RunSummary(Total = 1);
let outputHelper = new TestOutputHelper()
outputHelper.Initialize(messageBus, test)
let testExec() =


let dispose testClass =
match testClass with
| None -> ()
| Some obj ->
match box obj with
| :? IDisposable as d -> d.Dispose()
| _ -> ()

let testExec() =
let config = this.Init(outputHelper)
let timer = ExecutionTimer()
let result =
Expand All @@ -203,6 +211,8 @@ type PropertyTestCase(diagnosticMessageSink:IMessageSink, defaultMethodDisplay:T

timer.Aggregate(fun () -> Check.Method(config, runMethod, ?target=target))

dispose target

match xunitRunner.Result with
| TestResult.True _ ->
let output = Runner.onFinishedToString "" xunitRunner.Result
Expand Down
41 changes: 40 additions & 1 deletion tests/FsCheck.Test/Runner.fs
Original file line number Diff line number Diff line change
Expand Up @@ -285,4 +285,43 @@ module Deprecated =

[<Property( Arbitrary=[| typeof<TestArbitrary1> |] )>]
let ``should use Arb instance on method preferentially``(underTest:float) =
underTest >= 0.0
underTest >= 0.0

// see https://github.com/fscheck/FsCheck/issues/514
// Dispose not called
module BugReproIssue514 =
open System
open System.Threading
open FsCheck
open FsCheck.Xunit
open global.Xunit
open Xunit.Sdk

type TestMessageBus() =
interface IMessageBus with
member _.QueueMessage _ = true
member _.Dispose() = ()

let mutable disposed = false

type DisposableTestClass() =

[<Property>]
member _.FakeTest (x:int) =
Check.One(Config.Quick, true)

interface IDisposable with
member _.Dispose() =
disposed <- true

[<Property>]
let ``should call Dispose on classes inheriting from IDisposable`` () =
let methodInfo = typeof<DisposableTestClass>.GetMethod("FakeTest") |> ReflectionMethodInfo
let typeInfo = typeof<DisposableTestClass> |> ReflectionTypeInfo
let assemblyInfo = typeof<DisposableTestClass>.Assembly |> ReflectionAssemblyInfo |> TestAssembly
let testCollection = TestCollection(assemblyInfo, typeInfo, typeof<DisposableTestClass>.Name)
let testClass = TestClass(testCollection, typeInfo)
let testMethod = TestMethod(testClass, methodInfo)
let testCase = new PropertyTestCase(null, TestMethodDisplay.ClassAndMethod, testMethod)
testCase.RunAsync(null, new TestMessageBus(), [||], new ExceptionAggregator(), new CancellationTokenSource()) |> Async.AwaitTask |> ignore
Check.One(Config.Quick, disposed)

0 comments on commit ad7124f

Please sign in to comment.