Getting started with pytest Transcripts
Chapter: Configuration
Lecture: Common settings in pytest.ini

Login or purchase this course to watch this video and the rest of the course contents.
0:00 In the chapter seven directory. We've got a settings folder. So I'm thinking this is settings directory is a little project so I've set it up
0:10 with a pytest.ini file a tests directory and a source directory to put some source and and so this these are something.py has a simple function in it
0:22 Test something calls the function and make sure that it returns the correct number oh expected equals shouldn't have that repeated.
0:32 It should be a test func, something func equals expected. So that should be right. So those are just a simple test simple project.
0:41 But let's let's look at this any file. So this is a project that is it isn't ready. It doesn't have any installer in it yet so far.
0:49 So I haven't made it into a py install a package. It's just some code, like a some python code and some tests to go with it.
0:57 But still these are some of the common settings I often see in my projects in the pytesting file, it starts off with this pytest bracket thing and
1:07 that's just any document, any syntax to say .ini syntax to say hey, this this section applies to pytest.
1:15 The first one I often add is addopts and addopt is a is a way to set up options, command line flags that you always want to use.
1:24 So we've talked about '-ra' which means I'd like to have extra reporting on everything except for passing tests. Things like skips and xfails.
1:34 I'd like to know why those were skipped or X failed at the end. Strict markers is where we say if you find a marker that's new,
1:44 that's not declared in the ini file. Please give me an error instead of a warning and the strict config we'll just
1:52 demonstrate the importance of that. It's similar. It's like right now if I were to misspell one of these.
2:00 So if I had a python paths instead of python path for instance, and that's really easy to do because this is kind of like test paths and python
2:08 paths when you need because you can have more than one here. So that's an easy one to do and I want to make sure that that shows
2:15 up as an error and not just silently ignored. So strict config says make sure everything in in your config is valid. Those are those flags, test paths.
2:28 I often put in place so what test paths does is it says hey py test if you get run at the at the top level directory at
2:37 like this directory in or down really anywhere. Look for test within the test directory. So if I say clear this 'pwd'.
2:49 I'm still in that top level directory go up a little bit Chapter seven settings. Okay, so if I'm in in this directory and say pytest by itself,
3:04 it says look in the test directory, that's what the test path says. If I give it a directory and say tests. It looks at the directory I gave it.
3:13 Of course. That's why I told it to look for tests. But if I don't tell it, it looks for everything in subdirectories.
3:20 So it'll look for tests within the source directory also. And I'd just like to save a little time and say,
3:25 hey, you don't have to look in the source directory. There are any tests there. So I'm just telling it to just look in the
3:30 test directory. And the python path is it seems kind of similar, right? It looks kind of similar.
3:36 It's saying that that the source code is in the source directory, but it's not really that. So what we have is when this test directory,
3:47 we've got test something and it says import something now. If I don't have this here. So let me comment this out.
3:55 What happens is if I go into the test directory and tried to run pytest something, I get a module not found error.
4:10 There's no module named something. And that's because python has a variable called python path and it's the place where it looks for things to import.
4:20 And that's often like things that you have installed or the current directory. If I had that other file here.
4:26 So if I let's copy this first directory something to to this directory. And then I ran the tests, it would find it. But that's not where it is.
4:37 I I wanted it in a different directory on purpose. I wanted to keep my tests and source separate.
4:43 What the python path allows you to do is to add an extra project directory or two or three. You can have multiple ones here and list them.
4:51 So that python can find python and pytest can find them. So pytest just adds these to the python path.
4:58 So now of course when I don't have that extra something.py, I can run it just fine because that python path was added.
5:07 So that's, that's what that's for. We don't have that with the cards project because the cards project is installed and it
5:16 doesn't, it can be imported because it's an installed module doesn't need to be found
5:20 anywhere. The last one is markers and we've talked about that and I listed a few of them that I often put in place.
5:26 So I often put a smoke marker in a project. I'll pick out a handful of tests that are or many tests that are quick tests
5:34 of the major parts so that I can pick out tests to run to just quickly validate that. I think the the system is pretty okay.
5:44 Isn't a complete test, but maybe a quick test so quick would weird work as well. Uh, bugs. Uh, I actually don't use this much,
5:52 but it's kind of interesting idea to just say the tests to reproduce issues. Maybe you can have a marker for that if
5:58 you want to run all of those to make sure all the all the previously fixed bugs are still fixed. That might work or slow this one I do use once
6:05 in a while. So a marker to mark some tests to use the. '-m' not slow to try not run those all the time addopts test paths,
6:14 python path and markers. These are things I often use in in my any file And even if I'm not using any markers,
6:22 I will almost always use this addopts section with strict marker, strict config and '-ra' these are settings I use all the time.


Talk Python's Mastodon Michael Kennedy's Mastodon