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

How to test session-scoped fixture clean-up when writing plugins? #4193

Closed
butla opened this issue Oct 18, 2018 · 5 comments
Closed

How to test session-scoped fixture clean-up when writing plugins? #4193

butla opened this issue Oct 18, 2018 · 5 comments
Labels
type: question general question, might be closed after 2 weeks of inactivity

Comments

@butla
Copy link

butla commented Oct 18, 2018

I'm trying to modify tests for pytest-docker. One thing that I'd like to add is testing that the session-scoped docker_services fixture (which spins up and later destroys a Docker Compose environment) gets rid of the Docker volume it creates.

All of the approaches I have in mind have problems:

  1. use the recommended black box testing and create a small test that will use the fixture.
    The problem: All of the names are generated randomly. I don't know ahead of time how the volume will be called, so I can't check if it's gone. I could count the volumes before the run and after the run, but that would be flaky if some other process on the machine is working with Docker in the background.
  2. write a test using the fixture, do some assertions in it.
    The problem: Any fixture or test I can create that references docker_services will have a smaller scope, so I can't run any test code after the fixture is cleaned.

Any thoughts on the subject? Is there something obvious that I'm missing?

@nicoddemus
Copy link
Member

nicoddemus commented Oct 18, 2018

Hi @butla,

I think you got the gist of it.

One solution I can think of is to write the volume id somehow to stdout or a known file location (inside tmpdir) during option 1. After testdir.runpytest_subprocess(), you can read the id and check that the volume does not exist anymore.

Others might suggest more elegant solutions.

@nicoddemus nicoddemus added the type: question general question, might be closed after 2 weeks of inactivity label Oct 18, 2018
@butla
Copy link
Author

butla commented Oct 18, 2018

I'm gonna submit a PR to the docs about testing plugins if we get an elegant solution or if this thread :-) I'll go with your suggestion - seems to be robust enough.

@nicoddemus
Copy link
Member

I'm gonna submit a PR to the docs about testing plugins

Great, appreciate it! 😁

@butla
Copy link
Author

butla commented Oct 28, 2018

So the approach I took [here] (https://github.com/AndreLouisCaron/pytest-docker/blob/master/tests/test_integration.py#L42) can be described like this:

  • the outer test (the test testing the test fixture :) ) prepares a name for the file that will be used to pass data from the inner test
  • the filename gets inserted into the string with inner test's Python code by string formatting
  • inner tests happen, they write something to the file
  • inner tests end, the outer test can retrieve whatever it needs from the file.

Any ideas for a more elegant aproach? If there aren't any, I'll submit that for the docs.

@nicoddemus
Copy link
Member

I think that approach is fine @butla, I've used it myself on the past.

@Zac-HD Zac-HD closed this as completed Dec 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: question general question, might be closed after 2 weeks of inactivity
Projects
None yet
Development

No branches or pull requests

3 participants