HTF icon indicating copy to clipboard operation
HTF copied to clipboard

Sharing resources (like a database pool) between tests

Open seagreen opened this issue 10 years ago • 5 comments

I have some tests that each need a database pool, but I'd rather not create an individual database pool within each test for performance reasons.

I'd like to write my tests like prop_foo :: a -> Property, and have some way to feed the result of my pool creation function mkPool :: IO a into each test.

Does HTF provide a way to do that and still use its TH test discovery? Even if one has to do discovery manually, is there a way to at least keep the same test settings and pretty results printing code as the rest of one's HTF tests?

seagreen avatar Aug 06 '15 22:08 seagreen

Good point! We are needing the feature as well. I have a plan how to implement it, but it will need some time.

skogsbaer avatar Sep 08 '15 13:09 skogsbaer

From a users point of view, it would be nice to write something similar to this:

-- define a TestResource for database connections
dbConResource :: TestResource
dbConResource = mkTestResource setup tearDown
    where
      setup = openDb "foo.db"
      tearDown = closeDb

-- write some test using this resource
test_userOk =
    withTestResource dbConResource $ \con ->
    ...

test_blub =
    withTestResource dbConResource $ \con ->
    ....

HTF then calls the setup function for the TestResource before the first test depending on the resource is run. HTF calls the tearDown function directly after the last test depending on resource has been run (prompt cleanup).

Internally, we need some restructuring in the TestManager (a test must somehow carry its resources to use) and in the ThreadPool (to allow for resource allocation and prompt deallocation).

skogsbaer avatar Nov 13 '15 21:11 skogsbaer

One more thing to consider: when implementing this feature, we should keep in mind #8 (benchmark support). For benchmarks, the setup function runs the reference benchmark (the teardown function does nothing). For this case, we also need to specify if tests should run concurrently to other tests or setup/teardown functions and if setup/teardown functions should run concurrently to other things.

skogsbaer avatar Nov 13 '15 21:11 skogsbaer

Just re-checking if this has already been implemented, or is this still being considered? I looked at HTF for the first time today, and was confused about the lack of any examples where test is being run in some sort of ReaderT Env IO monad.

After reading this issue, my takeaway is that each test is completely isolated from each other, and that each test will have to manually execute runReaderT action env within its own code. Did I understand this correctly?

saurabhnanda avatar Jul 12 '19 15:07 saurabhnanda

Yes, at the moment each test is run in isolation.

skogsbaer avatar Sep 10 '19 07:09 skogsbaer