Pytest series (1) - Quick Start and explain the basis

If you want to learn from scratch Pytest, you can look at this series of articles Oh!

https://www.cnblogs.com/poloyy/category/1690628.html

 

Foreword

  • There are two pure test test framework, pytest and unittest
  • unittest should be well known, but also the old framework, a lot of people used to do automation, or whether UI Interface
  • pytest unit testing framework is based on another unittest develop more advanced and better use of
  • Out interviews Ye Hao, Ye Hao speaking with others, pytest cells was significantly higher than the force unittest

 

Why Pytest

pytest's official website, which has the following characteristics:

  1. Very easy to use, entry is simple, rich document, the document There are many examples can refer to
  2. It can support a simple unit testing and functional testing of complex
  3. Supports parameterized
  4. Test is performed during certain tests may be skipped (skip), or failure of some of the expected failure case marked
  5. Support Repeat (rerun) failure case
  6. Support the operation by the nose, unittest test case writing
  7. Generate html reports
  8. Easy integration and continuous integration tool jenkins
  9. Example performed with support portion
  10. It has a lot of third-party plug-ins, and can be custom extensions

 

Installation Pytest

cmd run

pip install -U pytest

View version

pytest --version

 

Quick Start

#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
__title__  =
__Time__   = 2020-04-06 12:33
__Author__ = 小菠萝测试笔记
__Blog__   = https://www.cnblogs.com/poloyy/
"""

def func(x):
    return x + 1


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


class TestClass:
    def test_one(self):
        x = "this"
        assert "h" in x

    def test_two(self):
        x = "hello"
        assert hasattr(x, "check")

Then, cmd file into the current directory, direct execution

pytest

 

Knowledge Point

  • If you do only  pytest  , looks in the current directory and its subdirectories to   test _ *. Py   or  * _test.py  file, find the file, found in the file in order to   test  the beginning of the function and execution
  • If you want to execute a file, you can  pytest start.py 
  • Plus -q, it is to display a simple result:  pytest -q start.py 

 

Pytest design principles used in Example

With Pytest write use cases, be sure to follow the rules to write, or do not conform to the rules of the test will not be implemented

  • Filename test _ *. Py files and * _test.py
  • To   test_  functions that begin with
  • In   Test  class beginning, can not contain  __init__  method
  • In   test_  class at the beginning of the method inside
  • All entries must have pakege package __init__.py file

 

Example execution rules Pytest

Note that the following are telling the pytest execute commands in cmd

1, a directory of all use cases

pytest

 

2, the next execution of a use case a document py 

pytest script name .py

  

3, run inside of a function module start.py

pytest start.py::test_answer

 

4, which run start.py module, which is a method of testing the class

pytest start.py::TestClass::test_two
 

5, -m marker expression

 pytest -m login

Will run with  @ pytest.mark.login  decorator modified all the tests, and then start speaking later mark oh

 

6, -x to stop testing when an error is encountered

pytest start.py -x

 

7, -maxfail = num, when the number of errors reaches a specified number of cases, the test was stopped

pytest start.py --maxfail=1

 

Pycharm run Pytest

Usually write the code, it is written in Pycharm, how to run cmd may have been using it with patients, and now we take a look at how to run Pycharm in Pytest

  1. First of all, we need to go to settings which set the framework for unit testing Pytest
  2. If nosetests, then, is the right to run python scripts that run oh
  3. If you set unittest is based on the framework to run unittest

 

note

pytest is compatible with unittest script, use cases before writing the unittest framework can also be used to run pytest

 

Guess you like

Origin www.cnblogs.com/poloyy/p/12641505.html