Getting started with pytest Transcripts
Chapter: Configuration
Lecture: The non-test files in the cards application

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Let's take a look at the cards project itself that we had in our pytest course directory. So I'm in the pytest course directory,
0:10 there's a lot of stuff here, but let's take a look at the top. So we've got actually let's take a look at it in
0:17 VS code. We've got the cards project and there's a tox.ini file and read me and a pytest.ini and a pyproject.toml.
0:28 I did mention that these can be used tox.ini pyproject.toml are two of the files that can be used as alternates, but in this case we're just using tox.
0:38 We're not doing pytest Config within here. It's just, it's running tox as a test environment. So we didn't really cover tox within the uh,
0:46 the course, but the project has it here, the book covers it if you'd like to learn more. But the pytest ini file has the best pytest config,
0:55 we'll take a look at the pyproject.toml doesn't have. This is a flit related information and project related,
1:03 but I don't have the pytest stuff in there. So pytest right now is stored in the pytest any within the test directory
1:10 We've got a conftest. Those are top level fixtures used by the entire test suite and then within the cli there is, there are __init__,
1:21 there's nothing in them. But there's a conftest that at the Cli level that uses there's fixtures that are just for cli testing.
1:29 Then the API, there is no conftest, so I didn't feel the need to have another level.
1:35 Actually, the API level fixtures are at the top level because there used by both by both the Cli and the API. Or the Cli and API tests.
1:45 That's the cards project. Feel free to play with that. Where are we at again? In pytest course,
1:53 I'm gonna go into the cards project clear and the test directory is there so I should be able to run pytest.
2:04 Oh, I've got errors. So what are those errors fixture, faker not found? Oh yeah, the project here. The full test suite also uses the faker package.
2:16 So if we install faker then our tests should run. Yes. So you can take a look at these, we didn't talk about the cli tests within the course,
2:32 but you might find those interesting. That is the pytest or the cards project tests that I wouldn't know what I
2:39 wanted to show you. So if we run the tests here and we look at the head at the top, there's interesting information,
2:47 it tells us the faker plugins there but it says that the root dir is this card's project. And the Config file is pytest.ini and test passes tests,
2:59 we'll talk about test pass here in a minute. Now what if I go into one of the directories. So if I do tests/API and I run the test from here
3:10 I just run these tests, it still tells me that this rootdir is at the same level and the Config file
3:20 as Pytest.ini. So what pytest does is it walks up from where I am and it, it looks in this directory and it says, is there is there a Config file here?
3:31 If there's not, it keeps going up a directory until it finds a config file it first looks in each directory up, it first looks for a pytest.ini.
3:40 If you can't find that, it looks for one of the other projects that has a pytest. Config in it. And if it finds a py test,
3:47 Config that's your that's your root directory. And so what does that mean? If I run the tests from here,
3:55 it it still finds the configuration and the fixtures from the top level.
4:00 That's one of the reasons why I recommend always using a config file like pytest.ini because if it didn't find one at the course level,
4:09 it would keep looking and look at the project level and then look at the okken level and look at users level.
4:14 And if I had a pytest ini file in one of those directories, I'm sure it would not be correct for this project.
4:21 So I always stick a pytest.ini file around or another configuration that has a pytest.ini setting. Even if that's empty just to make sure it's there.
4:32 So we're way down in the API. If we were to even create an empty pytest.ini and we run, it's not gonna work because it can't find the fixtures.
4:46 Let's just look at the head. It'll say this is the prime root directory, the Config files, pytest.ini and but it didn't find all the fixtures it
4:59 needed so it couldn't run the tests so we'll remove that pytest.ini. So let's take a look at the settings that I think are really common,
5:11 the settings that I usually put into config files.

Talk Python's Mastodon Michael Kennedy's Mastodon