question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

shouldFail with "unexpected result" instead of ignore

See original GitHub issue

Description

Using shouldFail is a little bit tedious when it comes to having to use ignore.

In my opinion, would be nice if shouldFail could take a function that had a return value (i.e. unit -> 'a) and then use that return value in the error message if the test would fail.

Repro steps

  1. Create MyTests.fs

  2. Insert code:

    module MyTests
    
    open System
    open NUnit.Framework
    open FsUnitTyped
    
    [<TestCase("foo")>]
    [<TestCase("42")>] // valid int, shouldFail will trigger
    let ``Throws on invalid int`` input =
        // want to do:
        // let operation () = Convert.ToInt32 input
        let operation () = Convert.ToInt32 input |> ignore
        shouldFail operation
    
    [<Test>]
    let ``Parses int`` () =
        Convert.ToInt32 "123" |> shouldEqual 123
    
  3. Run tests

Expected behavior

Error from first test should show:

 Throws on invalid int("42")
   Source: UtilityTests_Math.fs line 13
   Duration: 53 ms

  Message: 
    System.Exception : Operation did not fail.
    Unexpected result: 42
  Stack Trace: 
    TopLevelOperators.shouldFail[exn](FSharpFunc`2 f)
    UtilityTests_Math.Throws on invalid int(String input) line 13

Actual behavior

Error thrown:

 Throws on invalid int("42")
   Source: UtilityTests_Math.fs line 13
   Duration: 53 ms

  Message: 
    System.Exception : Operation did not fail.
  Stack Trace: 
    TopLevelOperators.shouldFail[exn](FSharpFunc`2 f)
    UtilityTests_Math.Throws on invalid int(String input) line 13

Related information

  • Operating system: Windows
  • FsUnitTyped: 2.3.2
  • .NET Runtime, CoreCLR or Mono Version: .NET 4.5.1

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
CaptnCodrcommented, Nov 3, 2020

@jilleJr You can close this issue. There is no plan to do it. Actually, there is no great need to use it. There are more ways to test code using other operators.

0reactions
jilleJrcommented, Nov 3, 2020

Oki ^^

Read more comments on GitHub >

github_iconTop Results From Across the Web

Should a program fail on errors or silently ignore them
Whether the program should fail or not depends a lot on the context. ... If it is an unexpected error, then your program...
Read more >
Testng on IntelliJ no output after tests are ignored
but ignoring tests is a bad practice. And test SHOULD fail and stop the execution on unexpected exceptions!
Read more >
How do I ignore errors in the play however it should fail at ...
Use ignore_errors : true for tasks you want to continue, but save their status with register . Then at the end of the...
Read more >
An unexpected call to one of the method of a mock doesn't ...
I only mix it into my test suite. This workaround both reports the unexpected calls directly as well as records them and at...
Read more >
Investigating JUnit 5 asserts - Alexander on Test
3 should fail until we use 'equalsIgnoreCase'; 4 should always fail. To avoid too much code, and allow me to see the results...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found