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

Accessing Pytest fixtures from Engine #7

Open
timmartin opened this issue Jun 7, 2023 · 5 comments
Open

Accessing Pytest fixtures from Engine #7

timmartin opened this issue Jun 7, 2023 · 5 comments

Comments

@timmartin
Copy link

I'm testing some Django code, and I'd like to use the Django test client. This is available in Pytest via pytest-django providing a client fixture.

It's meant to be used like this:

def test_with_client(client):
    response = client.get('/')
    assert response.content == 'Foobar'

I think the most natural way to adapt this to the Hitchstory approach is to add the decorator to my test_foo Pytest function:

def test_foobar(client):
    hs.named("Something with a client").play()

but it's not obvious there's a clean way to pass the client object through so that the engine can get at it. I think the client is recreated with each test (it potentially holds state) so we don't want to create the client once and include it when instantiating the engine.

@crdoconnor
Copy link
Contributor

crdoconnor commented Jun 7, 2023 via email

@timmartin
Copy link
Author

Does that mean instantiating the StoryCollection once in each test function too? I can't see any way to replace the engine on a StoryCollection. I assume there's no state held on the StoryCollection object that would make that a problem?

I've had a go doing it that way and it seems to work fine for now, so I'm not blocked on this.

Yes, adding the fixtures to the add_pytests_to looks like a good longer-term solution. I'm not actually using add_pytests_to since it's not featured in the Pytest examples in the docs, and I can't find it anywhere in the docs even by searching for it. I don't know if it's undocumented at the moment or if I missed something.

@crdoconnor
Copy link
Contributor

crdoconnor commented Jun 7, 2023 via email

@timmartin
Copy link
Author

It occurred to me that one limitation of including it as an option in add_pytests_to is that then the fixture gets added to all the cases, whether it's needed or not. If the fixture has some performance impact (e.g. I think adding the Django DB fixture has a performance overhead and limits the ability to run tests concurrently) then it might be desirable to control which tests get the fixture.

Is it reasonable to allow adding the fixture in the story definition?

@crdoconnor
Copy link
Contributor

crdoconnor commented Jun 8, 2023 via email

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