Pytest is a test framework in python. To install pytest, we need to use the command pip install pytest. After installation, we can verify if python has been installed by the command pytest –version. The version of pytest shall be known.
Pytest can be used for creating and executing test cases. It can be used in wide range testing API, UI, database and so on. The test file of pytest has a naming convention that it starts with test_ or ends with _test keyword and every line of code should be inside a method which should have a name starting with test keyword. Also each method should have a unique name.
def test_f(): print("Tutorialspoint")
To run the above code, we need to move to the terminal and use the command py.test. However this will not give much details from an execution point of view. To get information on execution we should use the command py.test –v. Here v stands for verbose.
In order to print the console logs, we need to use the command py.test –v –s. Again, if we want to run tests from a specific pytest file, the command is py.test <filename> -v.
The advantages of pytest framework are listed below −
Pytest is capable of executing multiple test cases simultaneously, thereby reducing the execution duration.
Pytest is capable of skipping a test method from a group of test methods during execution.
Pytest is free and does not have licensing cost.
Pytest is quick and easy to learn.
Pytest can choose to run a particular test method or all the test methods of a particular test file based on conditions.
Pytest is capable of skipping a few test methods out of all the test methods during test execution.
Pytest can be used to test a wide range of applications on API, database and so on.