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.

Issue calling Setup/Cleanup in tests

See original GitHub issue

Normally you can do some setup and tear down before and after your tests:

testOptions in Test += Tests.Setup( loader => ... )
testOptions in Test += Tests.Cleanup( loader => ... )

Note: When forking, the ClassLoader containing the test classes cannot be provided because it is in another JVM. Only use the () => Unit variants in this case.

So when your tests are forked like Play’s you have to do this:

testOptions in Test += Tests.Setup( () => println("Setup") )
testOptions in Test += Tests.Cleanup( () => println("Cleanup") )

I want to start a DB process before my tests run and stop the process after. The issue I’m having is that I don’t know how to share a variable between setup and cleanup so that I can take the DB that I started in setup and call the stop method on it in cleanup.

A suggestion would be to have Tests.Setup and Tests.Cleanup replaced by a Tests.Hook which takes a class providing Setup and Cleanup methods. This class could then have member variables shared between the two functions

Issue Analytics

  • State:open
  • Created 8 years ago
  • Comments:19 (17 by maintainers)

github_iconTop GitHub Comments

1reaction
eed3si9ncommented, May 9, 2018

For anyone looking for global setup, test task across all subprojects, global cleanup, here’s a way of solving this.

lazy val bigTestSetup = taskKey[Unit]("setup for the big test")
lazy val bigTestCleanup = taskKey[Unit]("cleanup for the big test")

  commands += Command.command("bigTest") { state =>
    "bigTestSetup" ::
      "test" ::
      "bigTestCleanup" ::
      state
  }
  ThisBuild / bigTestSetup := {
    // do database setup 
  }
  ThisBuild / bigTestCleanup := {
    // do database cleanup
  }
0reactions
benhutchisoncommented, Oct 20, 2018

Im trying something based on @eed3si9n sketch above, but in my case the middle of the sandwich is run not test.It seems that if run throws an exception and exits non-zero, the cleanup step gets skipped?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Calling cleanup code in JestEnvironment::teardown() still ...
Bug Report I have some code that needs to be cleaned up after my Jest test runs, i.e. const Redis = require("ioredis"); const...
Read more >
Setup and cleanup at build time | Test Framework | 1.1.33
Implement this interface if you want to define a set of actions to execute as a post-build step. Cleanup runs right away for...
Read more >
Testing in Go: Clean Tests Using t.Cleanup - Ilija Eftimov ‍
t. Cleanup registers a function to be called when the test and all its subtests complete.
Read more >
How to ensure that database cleanup is always performed ...
There are two ways to do this. One is using TestInitialize and TestCleanup attributes on methods in the test class. They will always...
Read more >
Set Up and Tear Down State in Your Tests
override class func tearDown() { // This is the tearDown() class method. // XCTest calls it after the last test method completes. //...
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