Teach you the pytest test framework (1) – first understanding of the framework

[pytest] testing framework

Introduction

pytest is a testing [framework] for Python , similar to unittest, but more concise, direct, easy to use, more powerful, and compatible with unittest code.

It can be implemented, skip some use cases when executing use cases, re-execute use cases after execution failures, mark use cases that fail to execute, etc.

pytest has a wealth of third-party plug-ins, such as pytest-selenium (integrated selenium), pytest-html (perfect html test report generation), pytest-rerunfailures (failure case repeated execution), pytest-xdist (multi-CPU distribution) Wait.

pytest also supports custom extensions.

Install

pip install pytest

After installation, you can view the version with the pytest –version command

File, class and function naming and writing rules

Using pytest, files, classes, and functions cannot be named arbitrarily. They must be named according to their rules, otherwise pytest will not recognize them.

  1. The test file starts with test_ (it can also end with _test)
  2. The test class starts with Test, note that the first letter of Test should be capitalized
  3. The test class name is directly followed by a colon without ()
  4. A test class cannot have an init method
  5. Every function in a test class must have a parameter (self)
  6. The test function starts with test_, note that the first letter should be lowercase at this time
  7. Assertions can use basic assert

A simple pytest practice

First create a file called test_study.py. Note that the file name starts with test_, which conforms to the rules of pytest. Write a simple program in this file:

# coding=utf-8

def func(x):
    return x + 1

def test_answer():
    assert func(3) == 5

Here, test_answer() starts with test_, which conforms to the rules, but func(x) does not need to be named according to the rules, because we are testing the test_answer() function, if we want to test the func(x) function, we also need to name it according to the rules

There is no need to import pytest, and there is no need to call them after writing the two functions. Note that the first function is called in the second function, and the assertion is used, and the assertion statement is deliberately wrong. The correct value of func(3) should be 4, but intentionally written as 5 to create an error

When executing, do not execute it directly in pycharm, but enter pytesttest_study.py in the terminal of pycharm or in the command line window as an administrator (there are actually more than two execution methods, there are actually three, which will be introduced later in the document)

Execution result :
terminal l:

Command Line:

The results are the same for both, with the following results:

def test_answer():

>       assertfunc(3) == 5

E       assert 4 == 5

E        +  where 4 = func(3)

It tells us that the def test_answer() function has an error, the assertion should be 4, but the actual value given is 5.

execution way

Terminal execution

The first is to enter the command in the Terminal of pycharm to execute, such as pytest -s test_study.py -m=mn1 to execute
on the command line as an administrator

The second is to enter the command in the command line window as an administrator to execute, such as pytest -stest_study.py -m=mn1

main statement block execution

The third is to execute in the python code itself, which requires:

  1. Write the command in the main statement block
  2. Add .main after pytest
  3. Enclose parameters and file names in quotation marks
    such as pytest.main([“-s”, “test_study.py”, “-m=mn1”])

call to execute

If the call statement is written separately and is not placed in the main statement block, the use case can also be executed, such as the following writing:

# coding=utf-8
import pytest

class  Test_case () : 
    def  test_case4 (self) : 
        print( "Execute case 4" )

    def  test_case5 (self) : 
        print( "Execute case 5" )

pytest.main(["-s"])

However, this type of writing is very error-prone, and the use case is often executed multiple times. The reason is unknown. Therefore, this type of writing must be avoided. There are two ways to solve this problem:

  1. Put the execution statement in a separate file, as follows:
    Execute this file directly, and the corresponding use case can be executed correctly.

  2. Encapsulate the execution statement into a function, and then call the function, as follows:
    The execute.py file encapsulates the execution statement, and then calls it in main.py:

that’s it.

Special attention: If the above two methods are adopted, the use case is still executed multiple times. Usually, the execution statement is written outside the main statement block in a certain file. To emphasize again, that kind of writing method is easy to cause the use case to be executed multiple times. It must be strictly avoided.

A problem to be aware of

Whether in Terminal, command line, or in the main statement block, there must be no spaces at both ends of the equal sign when entering the execution command, such as: “-m = mn1”.

Although adding spaces at both ends of the equal sign is a common way of writing in python, it is not allowed by the grammar when using pytest, especially when writing commands in the main statement block, because it is written in the code, so It’s easy to habitually put spaces around the equals sign. (To be continued)

Learning resource sharing

Finally, I would like to thank everyone who read my article carefully. Watching the rise and attention of fans all the way, there is always a need for a courtesy exchange. Although it is not a very valuable thing, if you can use it, you can take it directly.

These materials should be the most comprehensive and complete preparation warehouse for friends who want to advance to [Automated Testing]. This warehouse has also accompanied me through the most difficult journey, and I hope it can also help you! Everything should be done as early as possible, especially in the technology industry, and the technical foundation must be improved. I hope to be helpful…….

Follow my official account below to get it for free!

Leave a Comment

Your email address will not be published. Required fields are marked *