Skip to content
Draft
Show file tree
Hide file tree
Changes from 3 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
6 changes: 3 additions & 3 deletions src/FsCheck.NUnit/FsCheckPropertyAttribute.fs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ type private NunitRunner() =
// doesn't look pretty in the cases where it turns out that there's truly nothing to write (e.g.
// when QuietOnSuccess is true, and the Property passed.
if not (String.IsNullOrWhiteSpace argsForOutput) then
printfn "%s" argsForOutput
TestContext.WriteLine(argsForOutput)
override __.OnShrink(args, everyShrink) =
printfn "%s" (everyShrink args)
TestContext.WriteLine(everyShrink args)
override __.OnFinished(_,testResult) =
result <- Some testResult

Expand Down Expand Up @@ -283,7 +283,7 @@ and FsCheckTestMethod(mi : IMethodInfo, parentSuite : Test) =
match testRunner.Result with
| TestResult.Passed _ ->
if not config.QuietOnSuccess then
printfn "%s" (Runner.onFinishedToString "" testRunner.Result)
TestContext.WriteLine(Runner.onFinishedToString "" testRunner.Result)
testResult.SetResult(ResultState(TestStatus.Passed))
| TestResult.Exhausted _ ->
let msg = sprintf "Exhausted: %s" (Runner.onFinishedToString "" testRunner.Result)
Expand Down
27 changes: 27 additions & 0 deletions tests/FsCheck.Test/FsCheck.NUnit/PropertyAttributeTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,33 @@ module ResultStateExceptionHandlingTest =
NUnit.Framework.Assert.Pass()
}

module TestContextFormatterTests =
type CustomType(value: int) =
member _.Value = value
override _.ToString() = $"CustomType({value})"

[<OneTimeSetUp>]
let setupFormatter() =
// Register a custom formatter for CustomType
TestContext.AddFormatter<CustomType>(fun (ct: obj) ->
match ct with
| :? CustomType as custom -> $"[CUSTOM:{custom.Value}]"
| _ -> ct.ToString())

[<Property(MaxTest = 1)>]
let ``should allow TestContext.WriteLine within property tests`` (value: int) =
// Create a custom type instance
let customValue = CustomType(abs value % 100)

// Write it to TestContext - this should use the custom formatter
// This demonstrates that users can use TestContext.WriteLine with custom formatters
// within their FsCheck property tests
TestContext.WriteLine("Testing with value: {0}", customValue)

// The property always passes, but we're testing that TestContext is accessible
// and custom formatters work when used directly
true

module ResultOutputTests =
[<Ignore("These should be run by the test below")>]
module TestModule =
Expand Down