The following is a transcription from Andrewโs TestFlix talk on โHow to Jumpstart Python Testing in 5 minutes?โ
About Andrew:
Andrew Knight, also known as โPandy,โ is the Automation Panda. Heโs a software testing and automation champion who loves to help people build better-quality software. In the past, heโs built large-scale test automation projects that run continuously and reliably. Currently, Pandy is a Developer Advocate at Applitools, where he helps folks do the best visual testing they can. He is also the Director of Test Automation University, which offers a multitude of free courses on software testing from the worldโs leading instructors. On the side, Pandy is writing a book on software testing, and he also leads development for Boa Constrictor, the .NET Screenplay Pattern.
Check out his tech blog at AutomationPanda.com, and follow him on Twitter at @AutomationPanda.
Huge shoutout to BrowserStack, our Exclusive sponsor for all community events and Premier sponsor for Conferences. We thank them for supporting us.
Letโs Dive in!
Why Python?
So, Python is hot right now and everyone loves it, but why do people love python so much? Well, some people just love the simple readable syntax of the language, while others are really in love with the community of people who use python.
Python is a great language for test automation because it’s super easy to get started. So, today Andrew is going to tell us how to jump-start Python testing in 5 minutes flat.
Test Framework – pytest
To start testing, we must first pick a test framework. Test frameworks enable you to write individual test cases in code, execute them together, and report, pass, or fail results.
Here, we will use โpytestโ, python’s most popular test framework. โPytestโ is loved because of its structure. It is simple and concise.
For this demonstration, weโll be using Python3.8.4 and pytest 6.2.2. โpytestโ is a third-party package. So, if you don’t already have it, you’ll need to install it with โpip install pytestโ.
The next thing to decide is, where to put your test code. Typically, you want to put your test code in the same project or repository as the python code. That way everything can be found in one place and all code can be version controlled together. However, sometimes automated tests should be put in their own repository, like when there are end-to-end tests that cover multiple projects or entities.
for this demo, Andrew has created a new python project with a package named โstuffโ and a module named โaccumโ
The module has a class named โaccumulatorโ, it has a property named โcountโ for getting the current count value, as well as a method named โaddโ for adding to the count
Let’s see how to use that accumulator class briefly.
Type in the following โ
You can quickly create one, saying:
and now youโll have the object
Initially, the count is 0.
But you can add to it. Adding without any parameters adds 1, but you can also explicitly add 3 also and the account goes up by 3.
One thing which youโre not allowed to do is, set the count directly, as that would cause an error.
Let’s write some unit tests for the accumulator class.
Create a new folder named โtestsโ in your project, and inside this folder create a new module named โtest_accum.pyโ
Inside the test module, we need to add some import statements.
1. we need to import โpytestโ to get some things from the test framework.
2nd, we need to import the accumulator class from the stuff package.
All of our unit tests will need to create accumulator objects. Instead of copying and pasting initializer calls everywhere, we can use a โpytestโ fixture to code object creation one time. The fixture is just a function named โaccumโ and it returns a new accumulator object. It also has a decorator named โ@pytest.fixtureโ to denote it as a fixture.
Our first test case can be short and sweet. It’s just a function named โtest_accumulator_ initโ.
Notice how it takes in an argument named โaccumโ, which is the same name as the fixture we just created.
When โpytestโ executes this function, it will look for a fixture named โaccumโ and when it finds the right fixture, it will call the fixture and inject its return value into the test function. Here in that way, fixtures act like setup steps.
This test then verifies that the accumulator’s initial count is zero
โpytestโ uses python’s raw assert command for assertions, there’s no need for special assertion calls. We can add several other unit tests to the same module, each should have a unique name and cover a unique behavior
Notice how each calls the โaccumโ fixture to create their own accumulator objects.
Now it’s time to run these tests. From the command line run โpython -m pytestโ from the project root directory.
โpytestโ will discover test functions and test modules using the test_ prefix. If all tests pass then you should see green dots.
Let’s briefly inject a failure to see what happens when something goes wrong.
Upon failure โpytestโ will give you a big error, and it’ll show you exactly where, and how the failure happened.
Summing Up:
Python is indeed a popular language for test automation because of its simplicity and ease of use. In this post, we have seen how to jumpstart Python testing in just 5 minutes using the pytest framework. By following the steps outlined in this post, you can easily write and execute unit tests for your Python code. The use of fixtures in pytest makes it easy to create and initialize objects for testing, and the use of raw assert commands in pytest simplifies the assertion process. With the help of pytest, you can ensure the quality of your Python code and catch bugs before they cause bigger problems.