Getting started with pytest Transcripts
Chapter: Configuration
Lecture: Avoiding test file name collision with __init__.py
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
So I want to talk about the __init__ files and there is some confusion about these. The reason why they're used in within test directories.
0:09
Within source directories. There's a different story within source directories.
0:13
They're used for packaging. It's a python thinks of a directory of stuff as a package. Not a package to package,
0:21
but a package. If it has a __init__ in it, but if that doesn't make sense to you, don't worry about it. That's not the topic.
0:29
What we're talking about, the test directory. The reason why Dunder in it is around is sort of similar to say,
0:36
hey, python kind of imported like a package, but they don't have to, there doesn't need to be anything in it.
0:42
And the reason why I'm doing this is because right now is because I've got a test_add.py in both places.
0:50
And let's say I had a test add in both places and add from empty in both of them. I don't right now, but I could so if I had the same file name,
1:01
so this is our cards project still same file name in both places and I had
1:06
the same test name in both places and since I'm trying different things out in both
1:11
places, there's no reason why there shouldn't be so like test count, there's one here. Test count, there's not a duplicate name, but there could be,
1:20
you know what I mean? I've actually set up a situation within chapter seven where we do have duplicates in chapter seven.
1:27
There's a dupe subdirectory and I've got two directories under that. There's one test no init and test with init.
1:35
So these two directories are the same except for the presence of a net. So I've got a test. Any file also that's empty.
1:44
Just like I said before, just to tell pytest is that this is the top Don't go looking for it anywhere else.
1:51
But then we've got these test ads and they are duplicate tests. And so the API And the CLI Have the same test.
1:59
Obviously there would be different tests but the same test name. And test no test name here.
2:07
Why is this important? So this is important because we want to have this freedom and Yeah. So well let's just play with the difference of these
2:16
So if I'm in I mean dup chapter seven dup. Have these two directories. I go within the I could start it from the top
2:27
actually. But pytest test what I get is an error. It says file mismatch, imported module. Test add has has this file app tribute,
2:43
which is not the same as a test file we want to collect which is in So there's this two directory thing,
2:50
remove cache files or use a unique base name for your test file modules. So a unique base name. Well,
2:59
I'm just confused by this actually but it's not bad. It's like I know that something's wrong. And at least it lists these two directories.
3:08
So how do we fix this? This is not obvious from this error message. How to fix it. But the fixes with init.
3:16
So the __init__. If we do the same thing with the stiff the within that directory, it's fine. What's the difference in the first one?
3:29
We do know init? I can test subdirectories so I can test the API. And I can test the cli I just can't do it together. And this is frustrating.
3:43
And anyway, so that's why I recommend just sticking __init__ within your test subdirectories so that you don't run into that confusing message.