Python pytest test Framework Introduction Three

Source: Internet
Author: User

Previously introduced pytest in the form of xunit to write use cases, below to introduce pytest unique way to write use cases

1. Pytest Fixture Example 1

The code is as follows

 from __future__ Importprint_functionImportpytest @pytest. Fixture (Scope='Module')defResource_a_setup (Request):Print('\nresources_a_setup ()')    defResource_a_teardown ():Print('\nresources_a_teardown ()') Request.addfinalizer (Resource_a_teardown)deftest_1_that_needs_resource_a (resource_a_setup):Print('test_1_that_needs_resource_a ()') deftest_2_that_does_not ():Print('\ntest_2_that_does_not ()') deftest_3_that_does (resource_a_setup):Print('\ntest_3_that_does ()')

Use-S-V to run view details as follows

The use of Pytest's unique pattern to write use cases, using the scope method, scope support a variety of, will be introduced later

Pytest's Addfinalizer built-in features are also available here, which can be found on the official website and is used in the last Test project called Teardown

2. Pytest Fixture Example 2

The code is as follows

 from __future__ Importprint_functionImportpytest @pytest. Fixture ()defresource_a ():Print('\nresources_a () "Setup"') deftest_1_that_needs_resource_a (resource_a):Print('test_1_that_needs_resource_a ()') deftest_2_that_does_not ():Print('\ntest_2_that_does_not ()') deftest_3_that_does (resource_a):Print('test_3_that_does ()')

This is the simplest example, the results are as follows

It can be seen that the test case inherits the function of Pytest.fixture, and the function of Setup is executed, the default pytest.fixture

3. Several methods of using Pytest.fixture

There are several ways to use pytest.fixture to form a framework when writing use cases,

Method One:


This is the method mentioned above, as follows

pytest.fixture () def before ():     Print ('\nbefore each test'def  test_1 (before):    Print ('test_1 ()'def  test_2 (before):     Print ('test_2 ()')

Method Two: Use fixture to modify

@pytest. Fixture ()defbefore ():Print('\nbefore each test')@pytest. Mark.usefixtures ( "Before") deftest_1 ():Print('test_1 ()') @pytest. Mark.usefixtures ("before") deftest_2 ():Print('test_2 ()')

The red one is the decorator.

4. Scope parameters of fixture scope

Before using the @pytest.fixture (scope= ' module ') to define the framework, scope has the following parameters

    • Function Every use case is executed
    • Class executes each
    • Module execution (use case in function form)
    • Session is run only once per session, and during automated tests, the login step can use the session

The following is an example with a module

@pytest. Fixture (scope='Module')defresource_a ():Print('\nresources_a () "Setup"') deftest_1_that_needs_resource_a (resource_a):Print('test_1_that_needs_resource_a ()') deftest_2_that_does_not ():Print('\ntest_2_that_does_not ()') deftest_3_that_does (resource_a):Print('test_3_that_does ()')

Even though we inherited resource_a in each use case, in the actual test, all use cases performed only once resource_a

At this point, you may ask, why only this setup function, no teardown function, to teardown how to write, the method is as follows:

def cheese_db (Request):        ..... def teardown ():         Print ('\n[teardown] cheese_db finalizer, disconnect from DB')    Request.addfinalizer (teardown)

Here is the use of the previous introduction of the Request.addfinalizer function, the function name can be arbitrarily taken, not necessarily teardown

5, with the parameters of the fixture

We're not going to introduce you here, look at the official documents.

6, a variety of fixture scope combined use

Look at the code, as follows

@pytest. Fixture (scope="Module")deffoo (request):Print('\nfoo Setup-module Fixture')    deffin ():Print('Foo Teardown-module Fixture') Request.addfinalizer (Fin) @pytest. Fixture ()defBar (Request):Print('Bar Setup-function Fixture')    deffin ():Print('Bar Teardown-function Fixture') Request.addfinalizer (Fin) @pytest. Fixture ()defBaz (Request):Print('Baz Setup-function Fixture')    deffin ():Print('Baz Teardown-function Fixture') Request.addfinalizer (Fin)deftest_one (foo, bar, Baz):Print('In Test_one ()') deftest_two (foo, bar, Baz):Print('In test_two ()')

The test results are as follows

Pytest also has a very useful yield function, followed by the introduction

Python pytest test Framework Introduction Three

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.