pytest: everything you need to know about fixtures (intermediate) anthony explains

  Рет қаралды 63,539

anthonywritescode

anthonywritescode

Күн бұрын

Пікірлер: 49
@shekelboi
@shekelboi 8 ай бұрын
The way fixtures work is just so alien to me.
@TheMathues123
@TheMathues123 7 ай бұрын
learn decorators
@mfeickert07
@mfeickert07 2 жыл бұрын
This is a great introduction which I'll be sending to my students now. :) Thanks!
@bigmacbeta
@bigmacbeta 2 ай бұрын
Learnt something new. Use yield instead of return with the context manager in a fuxture. Thank you, thank you, thank you.
@sudharsan4040
@sudharsan4040 Жыл бұрын
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
@aneeszuberi1690
@aneeszuberi1690 Жыл бұрын
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?
@anthonywritescode
@anthonywritescode Жыл бұрын
pytest hooks up fixtures automatically and it passes them as arguments
@malakarakesh3139
@malakarakesh3139 11 ай бұрын
at 14:33 what's the difference between `return request.param` vs `yield request.param`?
@anthonywritescode
@anthonywritescode 11 ай бұрын
in this case nothing. I tend to use yield in case I want to convert it to a context fixture later on
@malakarakesh3139
@malakarakesh3139 Жыл бұрын
great videos. could you explain a lit bit more on the "state" with the temp dir as an example?
@JohnZakaria
@JohnZakaria 2 жыл бұрын
Test classes vs bare functions, when should I use classes
@IManu96I
@IManu96I 2 жыл бұрын
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
@anthonywritescode
@anthonywritescode 2 жыл бұрын
(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)
@cosmicallyderived
@cosmicallyderived Жыл бұрын
Didn’t know about the name kwarg for fixtures.
@petertiggerdine2631
@petertiggerdine2631 Жыл бұрын
Does the conftest inheritance work in the lasted verisons for pytest?
@swehba
@swehba 2 жыл бұрын
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?
@anthonywritescode
@anthonywritescode 2 жыл бұрын
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` (kzbin.info/www/bejne/h5updJujhq19rs0) 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 ... ```
@swehba
@swehba 2 жыл бұрын
@@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?
@ChimaChindaDev
@ChimaChindaDev 9 ай бұрын
How do you populate default db during a pytest run.
@GOZES
@GOZES 2 жыл бұрын
Something like this for the mock module from the standard library will be super useful :)
@anthonywritescode
@anthonywritescode 2 жыл бұрын
yep it's on the list -- hinted at in this video in fact
@iDontFinishAnyt
@iDontFinishAnyt Жыл бұрын
@@anthonywritescode Hopefully its coming soon! Would come super in handy.
@moiattube
@moiattube Жыл бұрын
Nice! Thanks a lot For a moment I thought you had autocompletion on -k option! Wouldn't it be great to have?
@anthonywritescode
@anthonywritescode Жыл бұрын
it's a substring match so idk how it would work
@natiachikovani7660
@natiachikovani7660 Жыл бұрын
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?
@anthonywritescode
@anthonywritescode Жыл бұрын
I wouldn't bother with unittest personally
@natiachikovani7660
@natiachikovani7660 Жыл бұрын
@@anthonywritescode Neither me! I wanted to hear exactly this ❤️
@duke007x3
@duke007x3 10 ай бұрын
Please explain pytest hooks and plugins)
@empty_pit7920
@empty_pit7920 2 жыл бұрын
Great video, thanks! I would gladly watch a video about pytest hooks and plugins
@АртемБеляков-к7з
@АртемБеляков-к7з Жыл бұрын
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
@cosmicallyderived
@cosmicallyderived Жыл бұрын
It sounds like you would create composite fixtures that depend on other fixtures in a representative hierarchy that reflects the domain you’re in.
@АртемБеляков-к7з
@АртемБеляков-к7з Жыл бұрын
@@cosmicallyderived Please advise a tutorial on how to do it right
@Quarky_
@Quarky_ 2 жыл бұрын
Fixtures within a scope is pretty cool, didn't know about that :)
@essamgouda1609
@essamgouda1609 2 жыл бұрын
Your videos are extremely useful !!
@abhinchhabra250
@abhinchhabra250 2 жыл бұрын
Going over indirect fixtures would be cool 😊
@anthonywritescode
@anthonywritescode 2 жыл бұрын
I don't think people should use them
@alice-smith
@alice-smith 2 жыл бұрын
Love the new font-size :^)
@Keshas1
@Keshas1 Жыл бұрын
This is good can write a fixture with a class scope
@thejtoken
@thejtoken 2 жыл бұрын
Great video, thanks!
@mahanirvaantantra
@mahanirvaantantra 8 ай бұрын
pytest has a lot of abstractions. That's one thing I hate about pytest !
@dankelman9562
@dankelman9562 2 жыл бұрын
thank you so much!
@chubbytonio
@chubbytonio Жыл бұрын
Great video ! thx
@umutgumusluoglu3175
@umutgumusluoglu3175 4 ай бұрын
thats a great video thank you but can you write your codes a little slower i have hard time following your codes as beginner
@anthonywritescode
@anthonywritescode 4 ай бұрын
feel free to pause the video at any point! or watch at a slower speed. the speed isn't going to satisfy everyone so the video controls give you the power to get what you want out of it
@matthewritter1117
@matthewritter1117 Жыл бұрын
Thanks!
@hiyadmn9damy
@hiyadmn9damy 2 жыл бұрын
Thanks
@yprohoda
@yprohoda 2 жыл бұрын
dff
@amir.hessam
@amir.hessam 2 жыл бұрын
first comment
getting started with pytest (beginner - intermediate) anthony explains #518
13:19
Какой я клей? | CLEX #shorts
0:59
CLEX
Рет қаралды 1,9 МЛН
Intro to Python Mocks | Python tutorial
18:42
Red Eyed Coder Club
Рет қаралды 89 М.
15 Python Libraries You Should Know About
14:54
ArjanCodes
Рет қаралды 409 М.
Pytest #6.1: Фикстуры - Основы применения
12:36
Артём Шумейко
Рет қаралды 16 М.
oops I'm the pyuwsgi maintainer now (intermediate) anthony explains #579
22:55
python descriptors! (advanced) anthony explains #519
19:04
anthonywritescode
Рет қаралды 18 М.
25 nooby Python habits you need to ditch
9:12
mCoding
Рет қаралды 1,8 МЛН
8 Powerful Features You Didn’t Know About the Fixtures of Pytest
13:18
The Dev World - by Sergio Lema
Рет қаралды 2,4 М.
pytest's parametrize (beginner - intermediate) anthony explains #027
12:24
This Is Why Python Data Classes Are Awesome
22:19
ArjanCodes
Рет қаралды 820 М.