Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Howto use teardown on testlist or testfixure #409

Closed
brase opened this issue Jan 28, 2021 · 5 comments
Closed

Howto use teardown on testlist or testfixure #409

brase opened this issue Jan 28, 2021 · 5 comments

Comments

@brase
Copy link

brase commented Jan 28, 2021

Hi,

I need a teardown function called after all tests of a testfixture or a testlist finished. I tried to use a disposable but I have no idea where to put it. Am I doing something wrong or is there no chance to achieve something like this?

Here is my example code:

[<Tests>]
let otherTests =
    let testDb = testDbName TestDbId
    let oneTimeSetup() =
        printfn "OneTimeSetup"
        restoreTestDbScript testDb "C:\\tmp\\snapshot.bak"
        |> executeCmd

    let oneTimeTearDown() =
        let disposable = { new System.IDisposable with
                            member this.Dispose() =
                                printfn "disposed!"
                                dropTestDbScript testDb
                                |> executeCmd
                             }
        disposable

    testList "With OneTimeSetup and OneTimeTearDown" [
        let x = lazy oneTimeSetup()

        let withDatabase f () =
            x.Force()
            use db = connectionString testDb |> connect
            let r = f db
            r

        yield! testFixture withDatabase [
            "Runs query against test db",
                (fun db ->
                    printfn "Testrun1"
                    let results = Queries.getSomething db (System.Guid.Parse("8e5614f7-b437-424f-b3bd-8d515e4087b4"))
                                  |> Async.RunSynchronously

                    Expect.hasLength results 23 "Should return the right amount")

            "Runs another query against test db",
                (fun db ->
                    printfn "Testrun2"
                    let results = Queries.getSomething db (System.Guid.Parse("8e5614f7-b437-424f-b3bd-8d515e4087b4"))
                                  |> Async.RunSynchronously

                    Expect.hasLength results 23 "Should return the right amount")
        ]
    ]
@brase
Copy link
Author

brase commented Jan 29, 2021

Used the workaround with the additional test at the end but had to change it to sequenced. (source: #352 (comment))

Is there any other option?

@haf
Copy link
Owner

haf commented Jan 30, 2021

There are many ways to model it, but I'd make it two testLists with the outer one setting up and tearing down and being sequenced, the the middle test being an inner testList with all your tests. This is probably the easiest version.

You can also program with standard parallel/concurrent programming primitives, such as countdown latch and lazy and such, counting down each test once it fails/succeeds and when the latch reaches zero, killing the database.

@brase
Copy link
Author

brase commented Jan 30, 2021

Thank you!

There are many ways to model it, but I'd make it two testLists with the outer one setting up and tearing down and being sequenced, the the middle test being an inner testList with all your tests. This is probably the easiest version.

Will the inner tests then run parallel or sequenced?

@haf
Copy link
Owner

haf commented Jan 30, 2021

In parallel

@brase brase closed this as completed Jan 31, 2021
@brase
Copy link
Author

brase commented Jan 31, 2021

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants