F# canopy - how to use LiveHtmlReporter? - f#

I am trying to get F# and canopy to log tests in html files.
So here it says that all I need to do is:
open configuration
open reporters
reporter <- new LiveHtmlReporter() :> IReporter
This didn't work for me. I managed to start the LiveHtmlReporter by using Chrome to start it. Now I am struggling to make it save the reports after tests are finished.
When I try to use:
reporter <- new LiveHtmlReporter(Chrome, "C:\\") :> IReporter
let liveHtmlReporter = reporter :?> LiveHtmlReporter
liveHtmlReporter.saveReportHtml #"C:\" "report"
It throws an InvalidOperationException was unhandled error at me before it get to the tests and doesn't save anything. Besides that, when the tests run - I can see only context titles, and test names are not printed - just Pass or Fail without test name.
Another thing is taking screenshot on error - it also doesn't happen.
I think I am doing wrong at the very bottom of my code. What is going wrong?

I had the same problem. This should help.
reporter <- new LiveHtmlReporter(Chrome, configuration.chromeDir) :> IReporter
let liveHtmlReporter = reporter :?> LiveHtmlReporter
liveHtmlReporter.reportPath <- Some "reports/AutomationResults"

Related

Why does F# interactive console not consider "assert (2=3)" as wrong?

I send this line to F# interactive console in Visual Studio
assert (2=3)
To my surprise, the console does not report errors, but
 
val it : unit = ()
Similarly, if I run
printf ("hello!")
assert (2=3)
printf ("hello2")
on an REPL, I got "hellohello2" without any error messages.
How could I make the F# interactive tell me 2=3 is wrong?
Under the cover, the assert keyword translates to a method call to the Debug.Assert method from the .NET library (see the method documentaiton). This has a conditional compilation attribute [Conditional("DEBUG")], meaning that the call is only included if the symbol DEBUG is defined.
By default, this is not the case in F# Interactive. You can do that by adding --define:DEBUG to the command line parameters for fsi.exe. This will be somewhere in the options of your editor, depending on what you are using. For example, in Visual Studio, you need something like this:
EDIT: How to do something like this if you do not want to modify the command line parameters? This really depends on what exactly behaviour you want. The default behaviour of assert is that it shows a message box where you can either terminate the program or ignore the error. You could do that using:
open System.Windows.Forms
let ensure msg b =
let res =
MessageBox.Show("Assertion failed: " + msg +
"\n\nDo you want to terminate the exection? Press 'Yes' " +
"to stop or 'No' to ignore the error.", "Assertion failed",
MessageBoxButtons.YesNo)
if res = DialogResult.Yes then exit 42
ensure "Mathematics is broken" (2=3)

InvalidCastException for Z3 in F#

I try to get the Z3 solver up and running in F#. So I created a fresh F# project in Visual Studio, added a reference to Microsoft.Z3.dll, and typed in the following code:
open Microsoft.Z3
let ctx = new Context()
let a = ctx.MkBoolConst("a")
Running this in the interactive window yields the following error:
System.InvalidCastException: Unable to cast object of type 'Microsoft.Z3.AlgebraicNum' to type 'Microsoft.Z3.BoolExpr'.
at Microsoft.Z3.Context.MkBoolConst(String name)
at <StartupCode$FSI_0013>.$FSI_0013.main#() in C:\Users\...\Program.fs:line 3
Stopped due to error
What am I missing?
This sounds very much like https://github.com/Z3Prover/z3/issues/1882
You might have to recompile/reinstall. Follow the instructions in that ticket.

What is wrong with this Akka.NET code?

I spent some time searching for the Akka.NET F# API. Could not find it, even though there is good C# documentation. I found the code below, dated March 2017, which looks good, but unfortunately generates an exception when I try to run it.
Two questions:
1) What is wrong with the code below?
2) Is there online documentation for the Akka.Net F# API and if yes, where is it?
Observation: I tried several other F# Akka.NET snippets I found online and all of them generated exceptions.
The URL for the code is:
https://www.seventeencups.net/building-a-mud-with-f-sharp-and-akka-net-part-one/
And here is the code I tried to run:
open System
open Akka.Actor
open Akka.Configuration
open Akka.Remote
open Akka.FSharp
let system = System.create "system" (Configuration.defaultConfig())
type GreeterMsg =
| Hello of string
| Goodbye of string
let greeter = spawn system "greeter" <| fun mailbox ->
let rec loop() = actor {
let! msg = mailbox.Receive()
match msg with
| Hello name -> printf "Hello, %s!\n" name
| Goodbye name -> printf "Goodbye, %s!\n" name
return! loop()
}
loop()
The exception message includes the following:
System.TypeLoadException: Method 'WatchWith' in type '-ctor#270' from assembly 'Akka.FSharp, Version=1.2.3.41, Culture=neutral, PublicKeyToken=null' does not have an implementation
WatchWith method has been introduced in Akka.NET v1.3, while you're using Akka.FSharp v1.2.3. You'll need to downgrade your Akka dependency back to 1.2.3 (at this point in time Akka.FSharp is not yet available in v1.3).

F# NUnit test sets value as null

I have 2 files: Asm.fs, AsmTest.fs
Asm.fs
namespace Assembler
[<Measure>] type ln
[<Measure>] type col
[<Measure>] type port
type Addr = int<ln> * int<col> * int<port>
module Asm =
let emptyAddr : Addr = (-1<ln>, -1<col>, -1<port>)
AsmTest.fs
module Assembler.Tests
[<Test>]
let someTest() =
let x = Asm.emptyAddr
When I debug someTest() code, I get that x = null, what am I doing wrong?
P.S. files in the different projects visual studio projects. Asm.fs in the Assembler project and AsmTest.fs in the AssemblerTest project.
I found an interesting behavior. My problem will be resolved, if I add some file (even empty) to the Assembler project. Can anyone explain this behavior?
The debugger sometimes has issues showing the correct values. For me however, having exactly the same Asm.fs and AsmTest.fs like this:
module Assembler.Tests
open NUnit.Framework
[<Test>]
let someTest() =
let x = Asm.emptyAddr
Assert.IsNotNull x
the test passes and if I set a breakpoint at the assertion, x is correctly shown as {(-1, -1, -1)}
As the code that you show does not compile as it is (Block following this let is unfinished. in someTest), could you try my test above and/or show your complete test method?
Using your code I can reproduce the behaviour. If I set my projects to console applications, my test will fail as well. So, it seems that for console projects, not having any other file or an [<EntryPoint>] surprisingly skips the initialization of module values. For me, the compiler at least issues a warning Main module of program is empty where I use x in the test. The solution to the problem is therefore:
make sure you treat warnings as errors
have an [<EntryPoint>] for console applications
use library projects for libraries

Debugger break location with F# exception handling

I came across this recently and wanted to check if there was something I was misunderstanding about F# exceptions. I am using Visual Studio 2012. Given the following F# code:
[<EntryPoint>]
let main argv =
try
raise (System.AccessViolationException("foo"))
printfn "Should not get here"
raise (System.AccessViolationException("foo"))
printfn "Should not get here either"
with | :? System.FormatException -> printfn "Should not get here"
This will raise and AccessViolationException which should not be caught by the with statement because the with only handles System.FormatException. I expected the debugger to be at the first raise when the exception occurs, but it is actually at the end of the with:
Now the problem is I can't see where the exception was raised from, also I can't see any information about the exception such as stacktrace. I don't think that I want to change the exception settings for individual exceptions because I would want to know the location of any unhandled exception (similar to C# or VB.NET) not when thrown.
If this is just the way that it is, then that is OK, but I just wanted to ask since I am not very familiar with F# and I wanted to make sure that I wasn't missing something. I had a look online and did not find anything about this.
Unless you want to modify your exception settings to break when that exception type is thrown (or a category of exceptions in the exception settings) then you need to make sure that your code takes care of unhandled exceptions however is most appropriate for your code.
try
raise (System.AccessViolationException("foo"))
with
| :? System.FormatException -> printfn "Should not get here"
| _ as ex ->
#if DEBUG
System.Diagnostics.Debugger.Break();
#else
printfn "Unhandled exception ('%s'): '%s'" (ex.GetType().Name) ex.Message
#endif

Resources