Python for the .NET developer Transcripts
Chapter: Testing
Lecture: Setting up pytest

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now, let's get started with pytest.
0:01 So, we come over here and create our test code
0:04 so we'll go over here and say test_lib
0:07 and we're going to test our lib.
0:08 And as it where we're going to write our test.
0:11 We're going to want to start by saying, import pytest.
0:14 And in fact, you don't
0:15 unless you're using some of the stuff
0:16 that we'll start using
0:17 the very, very beginning, we don't have to say that.
0:19 But notice, PyCharm says, mm, no pytest.
0:23 So we're going to go and add into our requirements
0:25 that are building up here pytest
0:27 and we're also going to have a couple more.
0:29 We're going to have to do mocking.
0:31 Remember, we had MOQ from the C# side.
0:35 So we're going to pytest_mock here.
0:38 And that's really sufficient, but we're going to have one more.
0:41 We'll have pytest-clarity.
0:44 pytest-clarity will give like a colored dif
0:46 if I, say, I expected this collection
0:49 or this string, but I actually got that other one
0:51 it will have better reporting for us.
0:53 And of course, let's tell PyCharm, pytest is not misspelled.
0:58 Up data in here, pip install -r requirements
1:01 or just click the button if you're in PyCharm.
1:05 Super, so, those three dependencies are installed here.
1:09 And let's just do a really simple test.
1:11 The way you do tests here is, we say test something
1:18 and then you have an assert statement.
1:20 Now, Python's assert statement has
1:23 generally, by default, nothing to do with testing or pytest.
1:27 It's just an assert, right?
1:28 It's part of, just, data validation that we could use.
1:31 But pytest co-opts that for its behavior.
1:34 So, let's say assert(7==9).
1:38 Now, if we want to run this, it's a little bit harder.
1:41 So, I could click Run here, but that's not
1:43 going to run the unit test in it.
1:45 We can do a couple of things.
1:46 Let's go over here, and say edit configuration
1:49 and we want to add a Python test, pytest thing.
1:54 We say script path and go over here.
1:59 But was it sufficient to do that, let's see.
2:02 Try to run that, see what we get.
2:05 Yeah, perfect! So we just pointed at the directory
2:07 and everything named test_something gets run.
2:09 Notice there's a problem here, the test failed.
2:12 7 is not equal to 9. We expected 9, we got 7.
2:15 See how it's not just, assert is false
2:17 when it should be true, but it actually gives you
2:18 a really nice error message.
2:20 So pytest is super smart like that.
2:22 It just uses this assert statement like so.
2:25 There's more complicated tests that we can do
2:28 like I expect an exception, or something like that
2:30 but if we're just doing this basic one
2:31 notice we're not even importing pytest.
2:33 We're just executing.
2:35 If you want to try to run this just from the terminal
2:38 you go here where our test file is and just type pytest.
2:42 And it runs again.
2:43 So that's how we get started with pytest.
2:45 You install it, you write something
2:47 with a method that is test_.
2:49 You have a file that is test_.
2:51 And then you do one or more assertions in the code.
2:54 Let's make that run.
2:56 You don't want to stop with a broken test, do you?
2:58 So we run this again, woo-hoo!
3:00 Looks like they pass this time.