pytest: everything you need to know about fixtures (intermediate) anthony explains
ฝัง
- เผยแพร่เมื่อ 23 ต.ค. 2022
- today I go over all the options and use cases for fixtures in pytest!
- testing a commandline with pytest: • python cli tested with...
- pytest parametrize: • pytest's parametrize (...
- pytest usefixtures: • why pytest.mark.usefix...
playlist: • anthony explains
==========
twitch: / anthonywritescode
dicsord: / discord
twitter: / codewithanthony
github: github.com/asottile
stream github: github.com/anthonywritescode
I won't ask for subscriptions / likes / comments in videos but it really helps the channel. If you have any suggestions or things you'd like to see please comment below! - วิทยาศาสตร์และเทคโนโลยี
The way fixtures work is just so alien to me.
learn decorators
This is a great introduction which I'll be sending to my students now. :) Thanks!
Great video, thanks!
I would gladly watch a video about pytest hooks and plugins
Your videos are extremely useful !!
Something like this for the mock module from the standard library will be super useful :)
yep it's on the list -- hinted at in this video in fact
@@anthonywritescode Hopefully its coming soon! Would come super in handy.
great videos.
could you explain a lit bit more on the "state" with the temp dir as an example?
Fixtures within a scope is pretty cool, didn't know about that :)
One of the best pytest tutorial. Can you please put a tutorial on mocking nested pytest classes and patching them. An excellent use case like using Azure or AWS python sdk
Going over indirect fixtures would be cool 😊
I don't think people should use them
How do you populate default db during a pytest run.
Love the new font-size :^)
Great video, thanks!
Does the conftest inheritance work in the lasted verisons for pytest?
Thanks a lot for the video.
If I am good with Pytest, should I know unittest too? Is it a must or I can be OK with only Pytest?
I wouldn't bother with unittest personally
@@anthonywritescode Neither me! I wanted to hear exactly this ❤️
Hi, while testing class TestMyThing; you only pasing fix fixture to test method, but in the result it is alos printing 'setup and tear down'. How is that happening?
pytest hooks up fixtures automatically and it passes them as arguments
at 14:33 what's the difference between `return request.param` vs `yield request.param`?
in this case nothing. I tend to use yield in case I want to convert it to a context fixture later on
thank you so much!
Nice! Thanks a lot
For a moment I thought you had autocompletion on -k option! Wouldn't it be great to have?
it's a substring match so idk how it would work
Great video ! thx
Hello Anthony! Could you tell me how to organize work with fixtures that return entities from the database (I load them at each start of the pytest)? The whole difficulty is that these entities depend on each other and are nested in each other. In an advanced project, keeping the database up to date in tests becomes hell. Mocks also don't save much as they also need to be nested inside each other
It sounds like you would create composite fixtures that depend on other fixtures in a representative hierarchy that reflects the domain you’re in.
@@cosmicallyderived Please advise a tutorial on how to do it right
Didn’t know about the name kwarg for fixtures.
Test classes vs bare functions, when should I use classes
I use them when a set of different test share a common set up, variables, fixtures or topic. So that the developer can find them better while providing unique fixtures that only applied to these.
This reduces the scope of some fixtures. If some of them are autoused, you can have better control on it for example
(for me): functions: always, classes: never
there's just too many ways classes can become unmaintainable (people trying to get too clever with test inheritance, accidentally misnaming a test class is way easier, weird accidentally stateful problems)
Thanks!
This is good can write a fixture with a class scope
Please explain pytest hooks and plugins)
Thanks
pytest has a lot of abstractions. That's one thing I hate about pytest !
dff
first comment
Anthony, love your videos. I always learn something. I've been using pytest for a while, and one thing that has always confounded me is how to add type annotation to a fixture factory function that an IDE like Pycharm can pick up. For example, let's say I have the following (admittedly simplistic fixture) that uses the `faker` package to generate person names with prefixes:
@pytest.fixture
def create_person_name_with_prefix(faker):
def inner(prefix: str = None, last_name: str = None) -> str:
prefix = prefix or faker.prefix_nonbinary()
last_name = last_name or faker.last_name()
return f'{prefix} {last_name}'
return inner
When I use the fixture factory, I'd like Pycharm to be able to call up the documentation (i.e., signature with types) of the inner function. If this were a standard decorator instead of `@pytest.fixture`, I could use `wraps` from the `functools` module, but that doesn't seem to work here. Is there another way to do something similar to `wraps` with pytest fixture factories?
pycharm I think added special code for pytest fixtures -- so you should be able to use standard type annotations here
I think you need a `Protocol` (th-cam.com/video/QjFChmQHJxk/w-d-xo.html) to represent this fixture return value due to the optional args:
```python
class InnerCallable(Protocol):
def __call__(self, prefix: str | None = None, last_name: str | None = None) -> str: ...
@pytest.fixture
def create_person_name_with_faker(faker) -> InnerCallable:
# actual implementation here
...
```
@@anthonywritescode Thanks for the lightning fast reply! I did as you suggested, and added type information to the outer/decorator function, and, sure enough, Pycharm sees that and displays it as documentation. However, as expected, it only displays the types, not the formal parameter names as it would with a non-decorated function. Any ideas about how to get the complete documentation (i.e. parameter names and types) to show?