Search code examples
f#xunitui-testingcanopy-web-testing

How to run a Canopy test suite with xUnit?


I decided to use canopy framework to test my UI.

Most of the examples include either built-in assertion framework or Expecto. Which are both good choices yet I use xUnit everywhere else in the project and want to be uniform for now.

For xUnit I have found only this example but it contains just two very basic tests whereas I need to things like common code to run prior to all the tests. What would be an idiomatic way for canopy + xUnit?


Solution

  • xUnit has class fixtures and here is an example how to bring it to F# code.

    Combining that with canopy scenarios, here is an example of what it can look like:

    open canopy.classic
    open canopy.types
    open System
    open Xunit
    
    let private openApp() =
        ...
    
    type Fixture() =
        do
            start ChromeHeadless
    
        interface IDisposable with 
            member _.Dispose() =
                quit()
    
    type Tests() = 
        interface IClassFixture<Fixture>
    
        [<Fact>]
        member _.``Soundcheck - Server is online``() =
            openApp()
            
        [<Fact>]
        member _.``Button1 is enabled``() =
            openApp()
    
            let button = element "#button1"
    
            Assert.True button.Enabled
    
        [<Fact>]
        member _.``Button2 is disabled``() =
            openApp()
    
            let button = element "#button2"
    
            Assert.False button.Enabled