Jumpstart Python Testing in 5 Minutes Flat by Andrew Knight

python testing

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. 

eRtF7xTjq jnyDYB5pbQiFcpRS2WSYo5yoNNjSHE39dsCyAvvxOSgfquktxLrIFkPZJsgx2ninSz5zvcjdG0zL RMbkBD6S4p KvJpZryHFgYUo6m

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”.  

python testing demonstration

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 – 

LYvIPEWCkSWwQD7tRpbq oCByUnKuDEWpUo7jjwhpsDUvM6M2 uw4Gnx4haDPyvFLSdD70uooO 0kvJEXbNf3wqtUrMxQKmYVcj7z7fAfr3IDwJ0eSXuvVn aaGBteIMbwCQDNzK5VfnWfNJI1AN2Q

You can quickly create one, saying:

1QDqK8UUGWwLDNk4bSfKZ gd J90yIKGoAHvejw6p4GaawrwCzhmU5 uLMd P P7Jd5xuqYGufxPRig6E8OA4uXgd5ftjJUQMmH9laFiM Nfb1DQHZmjPcQ2qOy2uI7jY0uY yG3vMCIWGWuThd98Q

and now you’ll have the object

eXhwkgBn7U9OwQuRPc727Nvt 1bLbVPSaxSkNpz5F4e2JJZ0thRonkFpQKJWUwY3j4lZ7f7q114Fua413D86N6c7UHzPrnbPBzxHgj4ol

Initially, the count is 0.

UZQR20r1i5ZDFiV4lQYxgy5dAsuOib7xRuOiRaOe72j0yq7yAreF7ndO CBDGJfTAHGsYVKINJ6koXTDHYSRyPXb0L1QKkIsN6Ceojc1nyf2K1GcruSdx

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.

aZyTWaqARO6A16iNOpxR92f684Hk38Bp4nyyxKoxOk1BT8H1VP RdCGt iEtGAnWD5ciFeK7EJw4GNKXdGuvVFCHdAp QaIoz fWIBQGKHasChRDLlsGBBazKkFqakrzwDUZqIoEoh9omz NXtU6ug

One thing which you’re not allowed to do is, set the count directly, as that would cause an error.

C8OP F9tI1UFlHA90rYzJ68

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”

LsxF0IkquvLUKt0U8SzLooN18OBvG7fPcVdpMwgp0mhQTDT5a0DssvIw7uSoxw91j1bFqOGRQloX4ApWFHc1ctCxROejKV1DlVSxSvdHffid hblkVIy3ZVKf066nl 8OGjQ6OHHBCQI5uIPcYUUrg

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. 

bQbPHiMfKjv040 zF7GI9d Xxy9 oJ50QVPlLCKDxbBX0It2lH1B HcGPyTje7ZMf

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.

2enKiqhaX2SwVYXqL8H6IICivNZQ7k2LKXxGVZGesxnD78zhITO 7sAqqno7R8l YaJ0q EVBGK7mZ fxMaX08sOFVAk3zIJcodxV S7rwbZF3RFsvAX D3ofQiqP6 8cD8Aaz Bh5wA2P2Odz4HbA

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

v4pElsRx1GQYOptfvIwU8gZ4BmewKy5t IYQ1b8xzBCHUs6WhW01f1wbIztAWvs bDw5RiB8RFKGtKfVW0JGbK9wFU1xMypqLyF46UveE5Yta Tbrnhc6fDgNvWc

“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

HgOel xSw MxQFdneY6BAkegU6o92K2vnwJmOJ053jr10GKI47g9TVO5dJsTMH16uQKgZ7E Zhc1n4GQjjPRPJWgvraTg3hjw4p0hTpfIPz

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. 

Ap8tKvf4y8rYHygQ51K KpQ0Aq3blYRMxxggQAL scB1YeGQWIRLQzLgQquG4tfpaqU zyxCIKdb EanjJ0sMCk3Ghfz8e8M8EGufSll8H0CakBFm eEFZ97kVZvNFEITd1 AEWZCA6CU 58kM2OXQ

“pytest” will discover test functions and test modules using the test_ prefix. If all tests pass then you should see green dots.

eSRQw OQRks8hDEnsLjhhvD4KgSwkB09kijmPE7QZcSQpTKKP6 v Yu8pR

Let’s briefly inject a failure to see what happens when something goes wrong.

jwU2vRaMW1fzyDUNwv5CyF46bzxnLYkf8MiNDR5rpJjGMhOSrxZLV9Zhk7joE67DSLgUUJDHvvAhsXeSBaVmc sgR2WqPnNpVcw1EkkIPja0oI R1aIDKi7Dv3XLRhIhQ9jc5gRYOeMAwL7lmAiig
LCZegslMDQsx8twZmflioYz9R44dLFx ne QtWq58ROehMhUGVpS800mEQlQ5IGwxpeLwlketgtyi7FXXYvnyqqT Fv3kAG0pPbcvnbGP9FVSJld97Xx8uEb6IaN97164 yjTCw5 LtN sQ5tODIqg

Upon failure “pytest” will give you a big error, and it’ll show you exactly where, and how the failure happened.

Sw1 IlcinuryrS5cCvsgOnr7QoBZGAXZSl4wDMBjIGIBNQfkAc07nmGpYXekgST9MPTihnSRmxHoegVt EjsJ gxC7 Re6EJaMKyXpPtF5NFoXK38j

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.

Leave a Reply

Your email address will not be published.

Related Posts