End to end Automation Test in Pytest

Testing of code is very necessary in order to maintain standard code quality and implementing test code seems to be a difficult task.

so to make developers work quite easy we can use tools for testing code and the most popular testing framework that allows users to write test codes using a python programming language is Pytest.

It helps users to write comprehensible test cases for databases, APIs, or UI. Pytest is commonly used for writing tests for APIs. It helps to write tests from basic unit tests to complex functional tests cases.

Uses of Python Pytest Automation?

Pytest is a feature-rich, plugin-based ecosystem for testing your Python code. and common tasks require less code and advanced tasks can be achieved through a variety of time-saving commands and plugins.

Uses of Pytest

  • Pytest is open-source.
  • Can run the test in parallel.
  • Automatically detect tests
  • Skip tests

End to end Automation Test in Pytest

Installation of Pytest

We can install pytest for windows in our virtual environment using pip –

pip install pytest

Get start with pytest

  1. open IDE (vs code or pycharm)
  2. Create a folder here we will create our test files inside this folder and navigate this folder in command line .For example : create a folder named start_pytest and create a file named test_1.py inside the folder
  3. your testing file should be in this manner –

      

def test_lowercase():
    assert "START PYTEST".lower() == "start pytest"

def test_reverse():
    assert list(reversed([44,55,66,77])) == [77,66,55,44]

def test_some_primes():
    assert 17 in {
        n
        for n in range(1, 20)
        if n != 1 and not any([n % div == 0 for div in range(2, n)])
    }

 

Here we have defined three functions test_lowercase() to convert uppercase string to lowercase using built-in function lower()
test_reverse() to reverse list items using built-in function reversed
test_some_primes() to check whether a given number is prime or not

save the above code before executing

we can execute the command pytest to get output

Here is the result of above code –

Thus, In this tutorial we have learned about unit testing with pytest, uses of pytest and the scope of pytest in detail.