Pytest – Conftest.py

Pytest – Conftest.py ”; Previous Next We can define the fixture functions in this file to make them accessible across multiple test files. Create a new file conftest.py and add the below code into it − import pytest @pytest.fixture def input_value(): input = 39 return input Edit the test_div_by_3_6.py to remove the fixture function − import pytest def test_divisible_by_3(input_value): assert input_value % 3 == 0 def test_divisible_by_6(input_value): assert input_value % 6 == 0 Create a new file test_div_by_13.py − import pytest def test_divisible_by_13(input_value): assert input_value % 13 == 0 Now, we have the files test_div_by_3_6.py and test_div_by_13.py making use of the fixture defined in conftest.py. Run the tests by executing the following command − pytest -k divisible -v The above command will generate the following result − test_div_by_13.py::test_divisible_by_13 PASSED test_div_by_3_6.py::test_divisible_by_3 PASSED test_div_by_3_6.py::test_divisible_by_6 FAILED ============================================== FAILURES ============================================== ________________________________________ test_divisible_by_6 _________________________________________ input_value = 39 def test_divisible_by_6(input_value): > assert input_value % 6 == 0 E assert (39 % 6) == 0 test_div_by_3_6.py:7: AssertionError ========================== 1 failed, 2 passed, 6 deselected in 0.09 seconds ========================== The tests will look for fixture in the same file. As the fixture is not found in the file, it will check for fixture in conftest.py file. On finding it, the fixture method is invoked and the result is returned to the input argument of the test. Print Page Previous Next Advertisements ”;

Pytest – Grouping the Tests

Pytest – Grouping the Tests ”; Previous Next In this chapter, we will learn how to group the tests using markers. Pytest allows us to use markers on test functions. Markers are used to set various features/attributes to test functions. Pytest provides many inbuilt markers such as xfail, skip and parametrize. Apart from that, users can create their own marker names. Markers are applied on the tests using the syntax given below − @pytest.mark.<markername> To use markers, we have to import pytest module in the test file. We can define our own marker names to the tests and run the tests having those marker names. To run the marked tests, we can use the following syntax − pytest -m <markername> -v -m <markername> represents the marker name of the tests to be executed. Update our test files test_compare.py and test_square.py with the following code. We are defining 3 markers – great, square, others. test_compare.py import pytest @pytest.mark.great def test_greater(): num = 100 assert num > 100 @pytest.mark.great def test_greater_equal(): num = 100 assert num >= 100 @pytest.mark.others def test_less(): num = 100 assert num < 200 test_square.py import pytest import math @pytest.mark.square def test_sqrt(): num = 25 assert math.sqrt(num) == 5 @pytest.mark.square def testsquare(): num = 7 assert 7*7 == 40 @pytest.mark.others def test_equality(): assert 10 == 11 Now to run the tests marked as others, run the following command − pytest -m others -v See the result below. It ran the 2 tests marked as others. test_compare.py::test_less PASSED test_square.py::test_equality FAILED ============================================== FAILURES ============================================== ___________________________________________ test_equality ____________________________________________ @pytest.mark.others def test_equality(): > assert 10 == 11 E assert 10 == 11 test_square.py:16: AssertionError ========================== 1 failed, 1 passed, 4 deselected in 0.08 seconds ========================== Similarly, we can run tests with other markers also – great, compare Print Page Previous Next Advertisements ”;

Execute a Subset of Test Suite

Execute a Subset of Test Suite ”; Previous Next In a real scenario, we will have multiple test files and each file will have a number of tests. Tests will cover various modules and functionalities. Suppose, we want to run only a specific set of tests; how do we go about it? Pytest provides two ways to run the subset of the test suite. Select tests to run based on substring matching of test names. Select tests groups to run based on the markers applied. We will explain these two with examples in subsequent chapters. Print Page Previous Next Advertisements ”;

Pytest – Environment Setup

Pytest – Environment Setup ”; Previous Next In this chapter, we will learn how to install pytest. To start the installation, execute the following command − pip install pytest == 2.9.1 We can install any version of pytest. Here, 2.9.1 is the version we are installing. To install the latest version of pytest, execute the following command − pip install pytest Confirm the installation using the following command to display the help section of pytest. pytest -h Print Page Previous Next Advertisements ”;

Pytest – Introduction

Pytest – Introduction ”; Previous Next Pytest is a python based testing framework, which is used to write and execute test codes. In the present days of REST services, pytest is mainly used for API testing even though we can use pytest to write simple to complex tests, i.e., we can write codes to test API, database, UI, etc. Advantages of Pytest The advantages of Pytest are as follows − Pytest can run multiple tests in parallel, which reduces the execution time of the test suite. Pytest has its own way to detect the test file and test functions automatically, if not mentioned explicitly. Pytest allows us to skip a subset of the tests during execution. Pytest allows us to run a subset of the entire test suite. Pytest is free and open source. Because of its simple syntax, pytest is very easy to start with. In this tutorial, we will explain the pytest fundamentals with sample programs. Print Page Previous Next Advertisements ”;

Pytest – Starting With Basic Test

Pytest – Starting With Basic Test ”; Previous Next Now, we will start with our first pytest program. We will first create a directory and thereby, create our test files in the directory. Let us follow the steps shown below − Create a new directory named automation and navigate into the directory in your command line. Create a file named test_square.py and add the below code to that file. import math def test_sqrt(): num = 25 assert math.sqrt(num) == 5 def testsquare(): num = 7 assert 7*7 == 40 def tesequality(): assert 10 == 11 Run the test using the following command − pytest The above command will generate the following output − test_square.py .F ============================================== FAILURES ============================================== ______________________________________________ testsquare _____________________________________________ def testsquare(): num=7 > assert 7*7 == 40 E assert (7 * 7) == 40 test_square.py:9: AssertionError ================================= 1 failed, 1 passed in 0.06 seconds ================================= See the first line of the result. It displays the file name and the results. F represents a test failure and dot(.) represents a test success. Below that, we can see the details of the failed tests. It will show at which statement the test has failed. In our example, 7*7 is compared for equality against 40, which is wrong. In the end, we can see test execution summary, 1 failed and 1 passed. The function tesequality is not executed because pytest will not consider it as a test since its name is not of the format test*. Now, execute the below command and see the result again − pytest -v -v increases the verbosity. test_square.py::test_sqrt PASSED test_square.py::testsquare FAILED ============================================== FAILURES ============================================== _____________________________________________ testsquare _____________________________________________ def testsquare(): num = 7 > assert 7*7 == 40 E assert (7 * 7) == 40 test_square.py:9: AssertionError ================================= 1 failed, 1 passed in 0.04 seconds ================================= Now the result is more explanatory about the test that failed and the test that passed. Note − pytest command will execute all the files of format test_* or *_test in the current directory and subdirectories. Print Page Previous Next Advertisements ”;

Stop Test Suite after N Test Failures

Pytest – Stop Test Suite after N Test Failures ”; Previous Next In a real scenario, once a new version of the code is ready to deploy, it is first deployed into pre-prod/ staging environment. Then a test suite runs on it. The code is qualified for deploying to production only if the test suite passes. If there is test failure, whether it is one or many, the code is not production ready. Therefore, what if we want to stop the execution of test suite soon after n number of test fails. This can be done in pytest using maxfail. The syntax to stop the execution of test suite soon after n number of test fails is as follows − pytest –maxfail = <num> Create a file test_failure.py with the following code. import pytest import math def test_sqrt_failure(): num = 25 assert math.sqrt(num) == 6 def test_square_failure(): num = 7 assert 7*7 == 40 def test_equality_failure(): assert 10 == 11 All the 3 tests will fail on executing this test file. Here, we are going to stop the execution of the test after one failure itself by − pytest test_failure.py -v –maxfail 1 test_failure.py::test_sqrt_failure FAILED =================================== FAILURES =================================== _______________________________________ test_sqrt_failure __________________________________________ def test_sqrt_failure(): num = 25 > assert math.sqrt(num) == 6 E assert 5.0 == 6 E + where 5.0 = <built-in function sqrt>(25) E + where <built-in function sqrt>= math.sqrt test_failure.py:6: AssertionError =============================== 1 failed in 0.04 seconds =============================== In the above result, we can see the execution is stopped on one failure. Print Page Previous Next Advertisements ”;

Substring Matching of Test Names

Substring Matching of Test Names ”; Previous Next To execute the tests containing a string in its name we can use the following syntax − pytest -k <substring> -v -k <substring> represents the substring to search for in the test names. Now, run the following command − pytest -k great -v This will execute all the test names having the word ‘great’ in its name. In this case, they are test_greater() and test_greater_equal(). See the result below. test_compare.py::test_greater FAILED test_compare.py::test_greater_equal PASSED ============================================== FAILURES ============================================== ____________________________________________ test_greater ____________________________________________ def test_greater(): num = 100 > assert num > 100 E assert 100 > 100 test_compare.py:3: AssertionError ========================== 1 failed, 1 passed, 3 deselected in 0.07 seconds ========================== Here in the result, we can see 3 tests deselected. This is because those test names do not contain the word great in them. Note − The name of the test function should still start with ‘test’. Print Page Previous Next Advertisements ”;

Identifying Test files and Functions

Identifying Test files and Test Functions ”; Previous Next Running pytest without mentioning a filename will run all files of format test_*.py or *_test.py in the current directory and subdirectories. Pytest automatically identifies those files as test files. We can make pytest run other filenames by explicitly mentioning them. Pytest requires the test function names to start with test. Function names which are not of format test* are not considered as test functions by pytest. We cannot explicitly make pytest consider any function not starting with test as a test function. We will understand the execution of tests in our subsequent chapters. Print Page Previous Next Advertisements ”;

Pytest – File Execution

Pytest – File Execution ”; Previous Next In this chapter, we will learn how to execute single test file and multiple test files. We already have a test file test_square.py created. Create a new test file test_compare.py with the following code − def test_greater(): num = 100 assert num > 100 def test_greater_equal(): num = 100 assert num >= 100 def test_less(): num = 100 assert num < 200 Now to run all the tests from all the files (2 files here) we need to run the following command − pytest -v The above command will run tests from both test_square.py and test_compare.py. The output will be generated as follows − test_compare.py::test_greater FAILED test_compare.py::test_greater_equal PASSED test_compare.py::test_less PASSED test_square.py::test_sqrt PASSED test_square.py::testsquare FAILED ================================================ FAILURES ================================================ ______________________________________________ test_greater ______________________________________________ def test_greater(): num = 100 > assert num > 100 E assert 100 > 100 test_compare.py:3: AssertionError _______________________________________________ testsquare _______________________________________________ def testsquare(): num = 7 > assert 7*7 == 40 E assert (7 * 7) == 40 test_square.py:9: AssertionError =================================== 2 failed, 3 passed in 0.07 seconds =================================== To execute the tests from a specific file, use the following syntax − pytest <filename> -v Now, run the following command − pytest test_compare.py -v The above command will execute the tests only from file test_compare.py. Our result will be − test_compare.py::test_greater FAILED test_compare.py::test_greater_equal PASSED test_compare.py::test_less PASSED ============================================== FAILURES ============================================== ____________________________________________ test_greater ____________________________________________ def test_greater(): num = 100 > assert num > 100 E assert 100 > 100 test_compare.py:3: AssertionError ================================= 1 failed, 2 passed in 0.04 seconds ================================= Print Page Previous Next Advertisements ”;