What Is PyTest? PyTest Fixtures, Assertions, and More

What Is PyTest?

The term “PyTest” itself gives the hint that it is the combination of the two terms “python” and “test”, two of the highly popular terms in the tech world. So, the name clearly states that it has something to do with Python, the widely used programming language, and testing, a crucial part of the software development life cycle. You will get to know more about PyTest as you go through the further sections of this article.

What Is PyTest?

PyTest is a testing framework used to easily write & scale test codes using the Python programming language. Using it, you become capable of writing simple and scalable test cases for databases, UIs, or APIs. Though PyTest can be used for all types & levels of software testing, this framework is primarily used for writing simple unit tests to complex functional tests for APIs. The amazing experience of using PyTest is cherished by different groups of people including development teams, QA teams, independent testing groups along with individuals practicing TDD & open source projects. The extremely useful advantages of using PyTest have made projects (e.g. Mozilla, Dropbox, etc.) from all over the internet switch from unittest or nose to pytest.

The reason behind such immense popularity of PyTest is its design, like an extensible system that allows writing plugins easily. It already has a rich ecosystem with lots of plugins and extensions that are highly useful to solve various purposes. Let’s dive more into the details of this framework in further sections.

Why Should You Use PyTest?

It is evident that PyTest is a great testing framework but why should it be preferred to other tools? Well, its advantages can state the reasons so, let’s check them out.

  • It is open-source so you don’t have to worry about budget while using it and can use it the way you want.
  • Its simple and easy syntax allows you to easily get started with it.
  • It can skip tests.
  • It can automatically detect the tests.
  • Using it enables you to run tests in parallel.
  • You can select specific tests and subsets of tests from the program to make them run.

What Are The Prime Features of PyTest?

The features that make PyTest a preferred choice of many teams are the following -

  • No API is required to use it.
  • It is capable of providing useful information about failures without using any debugger.
  • You can use it to run doc tests and unit tests.
  • It comes with an ecosystem of useful plugins and extensions.
  • You can write it as a function or method.

How To Install PyTest in Linux?

Here we have jotted down the step-by-step method to install PyTest in Linux.

  • Create a directory using the command (mkdir <directory name>) with a name suitable for you. The Python files will get saved here.
  • Build a virtual environment, where the installation of specific packages will occur rather than in the whole system. Through a virtual environment, we can separate different Python environments for different projects.
  • Example: Suppose we have multiple projects that rely on a single package like Django or Flask. Now a different version of Django or Flask may be used by each of those projects.
  • Now, if we upgrade a package in the global size packages, then it breaks into a couple of uses of websites that might not be what we have planned.
  • As a better scenario, each of these projects should have an isolated environment where they have only those dependencies and packages that they require and the specific versions they need.
  • That’s why virtual environments are built. They allow us to make different Python environments.
  • You can install the virtual environment via command line in Linux as mentioned below:
  • `pip install virtualenv`
  • Now, when the command `pip list` is run, the global packages installed globally in the machine will be shown with the specific versions.
  • `pip freeze` command is used to show all the installed packages with their versions in the active environment.
  • Use the command `virtualenv <environment name> –python=python` to build the virtual environment.
  • Then activate the virtual env by running the command: `source <environment name>/bin/activate
  • After the virtual environment is activated, install pytest in the directory that you created above.
  • Run the command: `pip install -U pytest` or `pip install pytest` (make sure that the pip version is the latest).

Example of A Basic PyTest Operation

Let’s see how to use PyTest using a basic example.

  • Create a folder study_pytest. We will save our test files inside this folder.
  • Now, navigate to that folder in your command line.
  • Create a file named test_sample1.py inside the folder.
  • Add the below code into it and save it.

import pytest
def test_file1_method1():
x=2
y=3
assert x+1 == y,"test failed"
assert x == y,"test failed"
def test_file1_method2():
x=2
y=3
assert x+1 == y,"test failed"

  • Use the following command to run the test.

py.test

The output will be shown as:

test_sample1.py F.
============================================== FAILURES ========================================
____________________________________________ test_sample1 ______________________________________
    def test_file1_method1():
    x=2
    y=3
      assert x+1 == y,"test failed"
>     assert x == y,"test failed"
E     AssertionError: test failed
E     assert 2 == 3
test_sample1.py:3: AssertionError

Here in test_sample1.py F.

F means failure

Dot(.) means success.

In the failures section, the failed method(s) and the line of failure can be seen. Here x==y means 5==6 which is false.

Source of example: https://www.guru99.com/pytest-tutorial.html#10

PyTest Fixtures

Fixtures are something that exempts you from running the same set of code before every test run. A common example of a fixture i.e. setting up a data source before a test is given below.

  • Before every test run, we have to set up a resource for that (The resources should be set up before the test starts and cleaned after the test is complete). For example, a test may need to access the database. So, a connection to the database should be made before the test and it should be disconnected after the test is complete.
  • Now, launch the URL and maximize the window before the test starts. Remember to close the window after the completion of the test.
  • Open data files to read or write. Here also remember to close them.

The property of fixtures is that they need to be run before and after each test function to which it is applied. As you saw above, they play a significant role in setting up and demolishing resources before the test starts and ends respectively. Now, let’s check out an example of a PyTest fixture.

You can identify a method as a PyTest fixture by the following mark -

@pytest.fixture

The way to use a Pytest fixture is mentioning the fixture as an input parameter.

Now, check out how to create a new file test_basic_fixture.py with the following code.

import pytest
@pytest.fixture
def supply_AA_BB_CC():
aa=25
bb =35
cc=45
return [aa,bb,cc]

def test_comparewithAA(supply_AA_BB_CC):
zz=35
assert supply_AA_BB_CC[0]==zz,"aa and zz comparison failed"

def test_comparewithBB(supply_AA_BB_CC):
zz=35
assert supply_AA_BB_CC[1]==zz,"bb and zz comparison failed"

def test_comparewithCC(supply_AA_BB_CC):
zz=35
assert supply_AA_BB_CC[2]==zz,"cc and zz comparison failed"

  • Here, a fixture named supply_AA_BB_CC that will return a list of 3 values.
  • There are 3 test methods comparing against each of the values.

Each of those test functions has an input argument whose name is matching with an available fixture. Then the corresponding fixture method is invoked by PyTest and the returned values will be stored in the input argument that is here the list [25,35,45]. Now, you can use the list items in test methods for the comparison.

Use this command to run the test:

py.test test_basic_fixture

The result will look like following -

test_basic_fixture.py::test_comparewithAA FAILED                                                                                                                                                                                      
test_basic_fixture.py::test_comparewithBB PASSED                                                                                                                                                                                      
test_basic_fixture.py::test_comparewithCC FAILED
                                                                                                                                                                                     
========================================== FAILURES ==========================================
_________________________________________ test_comparewithAA _________________________________________
supply_AA_BB_CC = [25, 35, 45]
    def test_comparewithAA(supply_AA_BB_CC):
    zz=35
>   assert supply_AA_BB_CC[0]==zz,"aa and zz comparison failed"
E    AssertionError: aa and zz comparison failed
E    assert 25 == 35
test_basic_fixture.py:10: AssertionError

_________________________________________ test_comparewithCC _________________________________________
supply_AA_BB_CC = [25, 35, 45]
    def test_comparewithCC(supply_AA_BB_CC):
    zz=35
>   assert supply_AA_BB_CC[2]==zz,"cc and zz comparison failed"
E    AssertionError: cc and zz comparison failed
E    assert 45 == 35
test_basic_fixture.py:16: AssertionError
============================= 2 failed, 1 passed in 0.05 seconds =============================

As zz=BB=35, the test “test_comparewithBB” is passed, and the remaining two tests are failed.

The scope of the fixture method is limited only within that test file it is defined. If any attempt is made to access the fixture in some other test file , an error will occur saying fixture ‘supply_AA_BB_CC’ not found for the test methods in other files.

Source of example: https://www.guru99.com/pytest-tutorial.html#5

Where Should You Add PyTest Fixtures?

As you are going through a lot of working information about PyTest, you should also know where to use it. You already know that fixtures are used in cases where some parts of code are reused multiple times. Hence, PyTest works as a great alternative to class xUnit style setup and teardown methods.

For better decision making about using fixtures, you can check for the following reasons that make it mandatory to use them.

  • As you can implement them in a modular manner, you don’t need to move through a large learning curve.
  • Fixtures come with good scope and lifetime. Similar to other normal functions, fixtures’ default scope is the function scope and the other scopes are – module, class, and session/packages.
  • Fixtures are reusable. Two of their popular use cases are unit testing and complex testing.
  • They act as vaccine and test functions. Those test functions are used by the fixture consumers in the fixture objects.

When To Avoid PyTest Fixtures?

Though Fixtures are capable of extracting the objects that are used in multiple test cases, they are not always the best choice to use. Even if your program needs a little bit of variation in the data, you must not consider using fixtures.

The Scope of PyTest Fixtures

PyTest fixtures let you know about the scope of using them when a function is invoked.

PyTests’ Future Scopes are -

  • Function: As previously mentioned, the function scope is the default value of Python fixture scope. The characteristic of this fixture is that it means a function scope gets executed only once in each session.
  • Module: In case of a fixture function that has a scope as a module, the function is created once per module.
  • Class: The cases where a fixture function is created per class object.

Assertions In PyTest

Assertions are such commands that are used to control the execution of certain methods based on a condition being true or false. That means the “assert” command can return either “True” or “False” status. In PyTest, if an assertion fails, the execution of the test method will stop at that point and the remaining code will not get executed.

Some Examples of PyTest Assertions:

assert "hello" == "Hey" is an assertion failure.
assert 5==5 is a successful assertion
assert True is a successful assertion
assert False is an assertion failure.

Conclusion

You got to know a lot about PyTest, a very strong effective testing framework based on Python, one of the most popular and dynamic programming languages. This article tells you about what it is, how to install it, how to start using it like a pro, and about PyTest Fixtures & assertions, two of PyTest’s highly important aspects that can exempt you from the hassle of repeating huge chunks of code in multiple occurrences and from the worst case scenarios of executing false tests.

However, this is a vast topic and we will cover more operational aspects of this popular testing framework in the future. For that, you just have to stay tuned to our blog page.

Also, in the tech world, products are developed using various different languages and infrastructure. But, whatever the case is, testing the products faster and without investing a lot of time & resources into writing complex test codes brings a significant level of efficiency to the business. And, all that is easily possible by automating your tests with a truly codeless test automation tool like PreFlight. You can get to know all the amazing features of this highly efficient tool from the article “10 Amazing Software Testing Benefits From PreFlight In 2022”.

To know more about this awesome tool, you can reach out to us anytime or visit our website. We are always eager to provide you with a demo of the magical testing experience for free.