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


Talk Python's Mastodon Michael Kennedy's Mastodon