Getting started with 1-pytest

Environment setup

  • Use pip to install pytest: pip install pytest
  • Original ecology report template: pip install pytest-html

Features of pytest framework

  • It is a third-party unit testing framework for python, which is more concise and efficient than unittest
  • Support more than 315 kinds of plug-ins, and compatible with unittest
  • When the unittest framework is migrated to the pytest framework, there is no need to rewrite the code

Naming rules

  • The py file should start with test_ or end with _test to name
  • The class must start with Test, and there can be no __init__ initialization method in the class
  • Method or function must start with test_
  • Asserts must use assert
  • General creation under project: lib package (place public package classes), data folder (place test files, documents, etc.), test_case package (place test cases), report folder (place reports)

Simple example

def test_login01():
    assert 1 + 1 == 2

def test_login02():
    assert 1 + 1 == 3

# 执行测试文件的固定格式,无需实例化类、或调用函数就可执行标准命名的类、函数
if __name__ == '__main__':
    pytest.main(['test_login.py'])
    

Execution result:
Insert picture description here
Among them,. Means passed and F means failed. FAILURES will show failed items

Guess you like

Origin blog.csdn.net/weixin_45128456/article/details/112534108