Getting started with pytest Transcripts
Chapter: Welcome to the course
Lecture: Topics covered
0:00 Let's talk about everything we're gonna cover in the course. First off we're gonna talk about test functions.
0:06 They're simple but powerful and we need to know some of the features of pytests that
0:11 helped make test functions so powerful and then we'll talk about test fixtures, parameterization, markers, plugins in configuration.
0:19 But really what are these things? Let's jump in quickly to kind of cover what these things are before we go too
0:25 much further. Test functions like shown here a simple test function that starts with test underscore and uses an assert around this.
0:33 We'll talk about test discovery, How does pytest find these things? We've said that pytest uses the built in python assert.
0:41 We'll look at these enhanced trace backs. Normally you fail a test with a certain and or pass a test with a passing
0:49 assert statement. But we can also use pytest fail and other exceptions, we'll take a quick look at those and sometimes we have code under tests that we
0:58 want to throw an exception in certain cases and we can test for that too.
1:03 We'll also briefly look at test classes because there are sometimes great reasons to run test
1:09 classes after that. We'll jump into fixtures what a fixture is is kind of like set up and tear down So unit test has set up and tear down methods but
1:18 pytest, you can use those but normally we use fixtures and in this case we're showing a dB fixture that database that has some set up the yield,
1:29 sends the database connection to the test and then the rest of it just hangs out until the test is done and then the tear down happens.
1:36 So we'll talk about set up and tear down and returning data. You can also use multiple fixtures protest and even fixtures can use fixtures.
1:45 Pytest comes with some built in fixtures that are pretty powerful and we'll take a
1:49 look at some of those. You can also scope fixture so what scoping means is a fixture can use run the setup and tear down phase after each test or it
2:00 can do it once per module or once per session and a whole bunch of other options. You can also share fixtures between tests and between test files.
2:11 If you throw the fixtures into a conftest.py file so we'll play with that parameterization is super powerful.
2:19 It's a way to write one test that covers many test cases in this example I'm
2:25 having this test finished test function that runs the test cases of a beginning starting state
2:31 starting in to do or in progress or done and then running the same test This is taking the place of three different tests all in one.
2:39 It's pretty cool. So we can talk about in this case parameters, tests will cover that but you can also parameters fixtures,
2:46 it's a little bit different syntax but we will cover that of course and using multiple
2:51 parameters can make it very powerful to use multiple parameters per row or you can stack these parameterizations so that you have a test
2:59 matrix. Markers are pretty powerful. So we'll cover those in this example. We are using the mark skip marker that will tell pytest to not run
3:08 a test. There's some reasons to use that, especially with skip if it's very powerful.
3:13 There's also X fail that will cover and you can use custom workers and those help
3:18 for selecting subsets of tests. You can also combine them and use markers plus fixtures And we'll definitely cover that because it's super powerful.
3:29 Next is plugins plugins are a way to extend pytest functionality and there's
3:34 some amazing third party plugins will cover x test is one that you can allows you to run tests and parallel to speed up testing.
3:43 And we'll also use 'cov' to collect test coverage. These are just too though there are hundreds of plugins available and there you can
3:53 even write your own. We will also talk about configuration because that's one of the
3:58 kind of a neat things about pytest is the configuration options You should have a
4:04 config file and we'll discuss why you need a config file but we'll also the config file could be a pytest any or Pyproject.toml, and we're going
4:12 to use pytest in this course and we'll also look at some cool command line flags. So one of the config options.
4:18 I'm showing is ad ops and that is a way to add flags to run every time, and there's good reasons to use some of them all the time,
4:27 and then other settings, like shown here as test paths and markers.