Pytest | Django | Gentle Introduction, Setup and Start Testing

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello my name is zander welcome to an introduction to pi test and the django framework this is a practical natured tutorial where we try and learn by example testing our django project using the pi test testing framework if you want to set up your environment to the exact specification to the machine that i'm working on here i'm going to be installing pi test 3.2.3 we're going to be utilizing windows 10 and we'll go ahead and pip install django which is currently 3.2 and i'm running python 3.9 so this isn't a requirement for you to run the software and follow the tutorial you will find that pi test will run for example on linux of course and on different versions of django and python pi test is probably the most widespread python testing framework there are many other python testing frameworks which you might use for specific use cases if you are new to testing it's worth reading around other frameworks to familiarize yourself with the spectrum of frameworks that are available and what benefits they offer i always believe it's good to keep an open mindset to ensure that we choose the right tools for the right task so as i've already said pi test probably being the most widespread testing framework in the python community probably second to that is unit test so i typically see new students gravitate to unit test as unit test is a module that comes with the python standard library so there's no additional install or setup needed to use unit test over time unit test is a very good tool it's a reliable solution for most projects however some would suggest that it lacks some of the convenient features that are available in the more advanced testing frameworks such as pi test convenience is probably the best word to describe pi test at least initially because i would consider it as easy to get started with and it has really simple syntax in actual fact an added benefit of pi test is that it is mostly compatible with unit test so if you are coming from unit test so if you started learning testing with unit tests it will be easy to migrate from unit test to pi test and in actual fact doing that you'll probably see the convenience that pi test can provide if you do want to follow this tutorial step by step you're going to need python installed and here i'm using visual studio code as my code editor just quickly giving you an overview of what's going to be covered in this tutorial we're going to start by creating a new project and then i will discuss a little bit about test folder structures in django we then go ahead and install pi test and all the other packages that are required to get started and then talk a little bit about the setup and pi test test discovery we can then go ahead and create our first test and run some tests with pi test so assuming you're new to django i'm going to just quick quickly take you through how to start a new django application if you are new to django just skip ahead to the next part of this tutorial so assuming you have python installed we go ahead and just create a new virtual environment so here i'm on windows remember so we've created a new virtual environment so vmv is the software say here um with the m flag and then the folder is going to be the emv so you can change that if you want to so now we just need to activate so let's go into the folder and go into the folder and activate your virtual environment and then we can pip install django okay so with that done i'm just going to go ahead now and type in django admin start project core and then space in a dot that should create a new folder inside of my project folder structure here called core so that's going to be my project my core project my django core project and inside here we have this setting so let's just create a a new project now inside of our core so change directory core and then let's just run the django admin again start project oh in this case sorry start app and then we'll call this app one so we're just going to build a new app we're going to put this inside of our core there it is and then just go to settings and we just need to register that of course app1 there we go so if we make any tables or database tables that would just get registered with django so we can access them from this app one so test folder structures in django so typically when you create a new application in django you're provided a test file so here we can play some tests now you might find tests in a number of different folders or areas so for example inside the app you might have a new folder called tests for example and inside of that you might then place your tests so that's one place where you might find tests inside of the actual application and of course in a lot of respects it makes sense to place the test as near as to the application as possible or to associate the test with the app by placing the test file inside of the application so it might be in actual fact you might have a separate test folder in the root directory here and this might be a a way of conjugating collecting all the tests from all the applications and running from in this folder so that might also be the case too so a lot of this is down to preference and because we're really beginning this journey of testing with pi test what we're going to do here what i'm going to do in this particular case is just place all the tests inside of this folder here called test that's on the root here and that's not for no particular reason other than to try and make them available for this tutorial series so think about where you want to put your tests normally you might see tests associated with applications within or closer to the actual application and potentially then you might want to then create a a separate testing area where you collect all those tests together because you might have multiple apps and then configure it from there so completely optional we're going to be running this test folder right here so now let's go ahead and install pi test now you will find pi tests on the package python package index so currently pi test 6.2.3 now i believe the developers of this application of this sorry package also build the pi test django so here we can see that it is fully supported here so pi test django 4.2.0 so in actual fact this is the application we want to install and this pi test django allows us like it says to test our django project applications using the pi test testing tool so if you like maybe this is a wrapper around django and pi test so when we install this it will actually also install pi test and this is a i guess we can think of this as an interface between django and pi test and it also provides us some additional options so we can work effectively with our django project and pi test so i'll just go ahead and pip install pi test django so once we've installed pi test django let's head over to the quick start tutorial guide so this is the documentation and it's worth having to read through further once you've completed this tutorial to have a look at some of the other features that it provides but it does provide us here a clear starting point in that we need to create a pi test.ini file so let's build this on our project root let's go back into our application and then the first setting that we're asked to apply is to configure pi test and point it to our project settings file so let's go ahead and do that so my project's called chord inside of my core is settings of course so that's now configured so secondly what we can now do is just define where pi test collects tests from and here we can define the actual file names that we want to use or associate to tests within our django project so let's go ahead and define what files we want to utilize or what naming conventions we want to utilize for our test files so here i'm going to be utilizing i'm going to remove this and this one here so all my tests in my project they're going to be named test underscore and then something associated to the tests so i know what the file is referring to the test file is referring to so in this case test underscore that's the mandatory part and then ex1 okay so that's now going to be picked up by pi test as a test because i've defined it right here now pi test will look for the test runner will look for all files that utilize this name convention within my whole project so this is the root directory and anything relative from here will look within all of these folders here for any tests so let's now go ahead and run a test so in my folder tests i've got a file called test underscore ex1.pi now inside of here all i need to do is import pi test assuming you have it installed of course and then from here i just need to build a simple function so let's call this test example and then what we need to do now is i'm going to creating this set so one equals one so without explaining this if that isn't familiar to you that now builds a very simple test i'm going to check that one equals one which of course it does that's going to be true so i would assume that my test is going to pass so all i need to do now is run pi tests from the terminal and you can see it says no module named app one just a little bit of debugging then apologies it was my own fault because i've got this app structure here i have forgotten to do two tasks uh first of all i just need to define where this app is because it's not in the root directory here in the project it's in the core so core dot and then because i'm using the app in this way i'm just going to get and remove the app store pi or in addition to that i can just add core to the name here either way that will work so now we've done that let's just go back to our test and run pi test again and there we go so no test run so it can always be frustrating when you're starting something new when it doesn't work by default so there's just a few things really that can go wrong here you haven't installed the packages django isn't isn't set up correctly that's what i tried to show you in the previous step there and then thirdly you're not in the right directory so here you can see that i'm currently the terminal is in the core directory inside of this folder here so what i need to do is change directory come out of that and go into the root directory and i need to run my test from the root directory so now i type in pi test there we go so let's take a look at the output here we have some environment variables at the top here nothing made to do with our actual tests and we've collected one test or one item and he identifies where the test runner has found tests what files it's found tests within so here inside of a test we have our test underscore ex1 file so the green dot here represents a single test that's been completed so if i were to for example run two tests and then run it again we're going to receive two dots now i'm going to need a different name so now we have two dots so that's indicating um the green dot being that the test has been successful now we have two passed and the time is taken to conduct the test that's always useful so with that said let's have a look at when tests fail so by changing assert to two here equal to two obviously that's going to be false so it's going to cause an error so let's go ahead and run our test and we now have produced a failure so it clearly identifies once failed one's passed now here you can see we have the green dot as a pass for the first test and the f indicating a fail in the second test we also have some additional information where the failure has occurred and you can see that is creating an assertion error so just a little bit about assertion conditions or certain conditions here python has a built-in assert statement to use assertion conditions in a program so a cert statement has a condition or expression which is supposed to always be true so let's just think of it as cert as true so in the test before we had assert one is equal to one so if that is true the test will pass so the general idea then is for us to set up the required data the input data call a function or method from the source code and then confirm by assert statement that the result conforms to the expectations so here for example imagine we've got in a django a basket in an e-commerce store so we collect all the data or we add item to that basket and then we'll go ahead and create an assertion statement so if there is something in the basket the basket quantity must equal one if that's true then we've just tested a a small component of our application so we've tested to see if the application does add something to our basket and we've used a simple assert statement here to test that so here we use assert in pi test and pi test is basically extending the output from an assertion error and providing us more information to help us understand where the tests are failing so it might be that you want to stop testing when there is a failure so here i've set this up so the first test will fail and the second test obviously will pass so let's go ahead and run pi test and let's use the x flag and you can see here that the test failed the test stopped running after the first failure was found so the second test wasn't actually run here so in addition to that there may be additional output that's uh generated from tests so let's just do a simple print here test one so if we wanted to output that we can extend again by using the report functions here so rp and that will then now output the print statement here so we can also specify which tests we want to run so for example if we know tests are placed in a certain folder or maybe we've broken our tests into different components and we only want to test one aspect maybe one application for example we can obviously specify that so let's run pi tests and here what we can do is just specify a folder for example so tess in this case and the test that the test run is going to look within that folder only so for example if i were to specify the core folder we know there's no tests inside that core folder so we don't run any tests so we can drill down further so just using the folder structure here we can move into test ex one dot pi and then we can then move into that file and run the individual tests here so just make sure we're in the right folder so uh pi test and then test this is the test folder inside of here we have a file called ex1 but pi and then we have a test here called test example one so we can just run that test if we want to so just to finish off this tutorial let's just quickly talk about pi test marks so a mark is basically a way of adding metadata to a test pi test has some built-in marks which we can utilize within our tests and we can build our own so you can head over to the documentation here so this is docs.test.org and this is mark.html so you can see that there's a few tests sorry there's a few marks here built-in markers that we can utilize so let's have a look at two here that skip and x-fail so there might be a particular situation where you want to skip a test maybe you know it's going to fail or it needs more development so here we can use the pi test declarators mark to skip a test so if i were to run this now we will have an indication that a test has been skipped one has been skipped and one has passed similar to this we can also identify where tests are failing or where we know tests are going to fail so we can use the x fail marker to do that let's go in and pi test again and you can see now we have one test that we know that's going to fail so in addition to that we can actually build our own markers and this can be fairly useful so here we define in our ini file here our pi test ini and marker so we give the marker a name so this is the identifiable name that we're going to use within the marker and this is just a string which identifies what this marker does so now we have this in place we can then go back into our test here and we can mark it in this case as slow and now what we can do is we have the option with pi test to identify these markers and partic only run these particular tests for example so we can use the m flag here and then let's just define slow so now all we're going to run is the tests that have been marked as slow there we go so that was a gentle introduction getting started and preparing to test with django and pi test in the next tutorial we're going to cover fixtures a very powerful tool that we have to set up data in preparation for testing thank you very much for listening hopefully this was useful and hopefully i'll see you in the next tutorial
Info
Channel: Very Academy
Views: 8,952
Rating: undefined out of 5
Keywords: pytest, py testing, python pytest, pytest django, django pytest, python testing with pytest, pytest quick start guide, testing with pytest, pytest tutorial, pytest python, pytest framework, pytest in python, pytest python3, pytest automation framework
Id: LYX6nlECcro
Channel Id: undefined
Length: 19min 13sec (1153 seconds)
Published: Tue Apr 13 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.