pytest分散式執行插件 pytest-xdist 的高級用法

来源:https://www.cnblogs.com/SoonPan/archive/2022/08/12/16578391.html
-Advertisement-
Play Games

想要使用多個CPU核心來進行測試,可以使用 -n 參數( 或者 --numprocesses) (使用8個核心來跑測試用例) pytest -n 8 使用 -n auto 參數可以利用電腦的所有核心來跑測試用例 測試時使用的演算法可以根據--dist命令參數定製: --dist load(預設選項): ...


想要使用多個CPU核心來進行測試,可以使用 -n 參數( 或者 --numprocesses)
(使用8個核心來跑測試用例)

 pytest -n 8

使用 -n auto 參數可以利用電腦的所有核心來跑測試用例
測試時使用的演算法可以根據--dist命令參數定製:

  • --dist load(預設選項):給每個CPU核心隨機分配用例,不保證執行順序。
  • --dist loadscope:對於測試函數,測試按模塊分組,對於測試方法,測試按類分組。每組作為一個整體分配給可用的worker。這保證了組中的所有測試都在同一進程中運行。如果的模塊級或類級fixtures,這將非常有用。按類分組優先於按模塊分組。
  • --dist loadfile: 測試用例按其所在文件分組。每組作為一個整體分配給可用的worker。這保證了文件中的所有測試都在同一個輔助進程中運行。
  • --dist loadgroup: 測試按xdist_group標記分組。每組作為一個整體分配給可用的執行器。這保證了具有相同xdist_ group名稱的所有測試都在同一個worker中運行。
@pytest.mark.xdist_group(name="group1")
def test1():
    pass

class TestA:
    @pytest.mark.xdist_group("group1")
    def test2():
        pass- 

這將確保test1和TestA::test2將在同一個worker中運行。沒有xdist_ group標記的測試在--dist=load模式下正常運行。

  • --dist no:正常的pytest執行模式,一次運行一個測試(完全沒有分發)。

例子:
項目目錄結構

xdist_test.py

import logging

import pytest


class TestXdist(object):

    @pytest.mark.xdist_group("group1")
    def test_one(self):
        logging.info("1")
        assert True

    @pytest.mark.xdist_group("group1")
    def test_two(self):
        logging.info("2")
        assert True

    @pytest.mark.xdist_group("group2")
    def test_three(self):
        logging.info("3")
        assert True

    @pytest.mark.xdist_group("group2")
    def test_four(self):
        logging.info("4")
        assert True

xdist_dummy_test.py

import logging

import pytest


class TestXdist(object):
    @pytest.mark.run(order=1)
    @pytest.mark.xdist_group("group1")
    def test_dummy_one(self):
        logging.info("d1")
        assert True

    @pytest.mark.xdist_group("group1")
    @pytest.mark.run(order=2)
    def test_dummy_two(self):
        logging.info("d2")
        assert True

    @pytest.mark.run(order=3)
    @pytest.mark.xdist_group("group2")
    def test_dummy_three(self):
        logging.info("d3")
        assert True

    @pytest.mark.xdist_group("group2")
    @pytest.mark.run(order=4)
    def test_dummy_four(self):
        logging.info("d4")
        assert True
(venv) ➜  pytest pytest -n auto --dist loadscope
=========================================================================== test session starts ===========================================================================
platform darwin -- Python 3.8.9, pytest-7.1.2, pluggy-1.0.0
rootdir: /Users/spock/PycharmProjects/pytest, configfile: pytest.ini
plugins: xdist-2.5.0, forked-1.4.0, ordering-0.6
[gw0] Python 3.8.9 (default, May 17 2022, 12:55:41)  -- [Clang 13.1.6 (clang-1316.0.21.2.5)]
[gw1] Python 3.8.9 (default, May 17 2022, 12:55:41)  -- [Clang 13.1.6 (clang-1316.0.21.2.5)]
[gw2] Python 3.8.9 (default, May 17 2022, 12:55:41)  -- [Clang 13.1.6 (clang-1316.0.21.2.5)]
[gw3] Python 3.8.9 (default, May 17 2022, 12:55:41)  -- [Clang 13.1.6 (clang-1316.0.21.2.5)]
[gw4] Python 3.8.9 (default, May 17 2022, 12:55:41)  -- [Clang 13.1.6 (clang-1316.0.21.2.5)]
[gw5] Python 3.8.9 (default, May 17 2022, 12:55:41)  -- [Clang 13.1.6 (clang-1316.0.21.2.5)]
[gw6] Python 3.8.9 (default, May 17 2022, 12:55:41)  -- [Clang 13.1.6 (clang-1316.0.21.2.5)]
[gw7] Python 3.8.9 (default, May 17 2022, 12:55:41)  -- [Clang 13.1.6 (clang-1316.0.21.2.5)]
gw0 [8] / gw1 [8] / gw2 [8] / gw3 [8] / gw4 [8] / gw5 [8] / gw6 [8] / gw7 [8]
scheduling tests via LoadScopeScheduling

test2/xdist_dummy_test.py::TestXdist::test_dummy_one 
test/xdist_test.py::TestXdist::test_one 
[gw0] [ 12%] PASSED test2/xdist_dummy_test.py::TestXdist::test_dummy_one 
[gw1] [ 25%] PASSED test/xdist_test.py::TestXdist::test_one 
test2/xdist_dummy_test.py::TestXdist::test_dummy_two 
test/xdist_test.py::TestXdist::test_two 
[gw1] [ 37%] PASSED test/xdist_test.py::TestXdist::test_two 
test/xdist_test.py::TestXdist::test_three 
[gw0] [ 50%] PASSED test2/xdist_dummy_test.py::TestXdist::test_dummy_two 
test2/xdist_dummy_test.py::TestXdist::test_dummy_three 
[gw1] [ 62%] PASSED test/xdist_test.py::TestXdist::test_three 
test/xdist_test.py::TestXdist::test_four 
[gw0] [ 75%] PASSED test2/xdist_dummy_test.py::TestXdist::test_dummy_three 
test2/xdist_dummy_test.py::TestXdist::test_dummy_four 
[gw1] [ 87%] PASSED test/xdist_test.py::TestXdist::test_four 
[gw0] [100%] PASSED test2/xdist_dummy_test.py::TestXdist::test_dummy_four
============================================================================ 8 passed in 0.40s ============================================================================
(venv) ➜  pytest pytest -n auto --dist loadfile 
=========================================================================== test session starts ===========================================================================
platform darwin -- Python 3.8.9, pytest-7.1.2, pluggy-1.0.0
rootdir: /Users/spock/PycharmProjects/pytest, configfile: pytest.ini
plugins: xdist-2.5.0, forked-1.4.0, ordering-0.6
[gw0] Python 3.8.9 (default, May 17 2022, 12:55:41)  -- [Clang 13.1.6 (clang-1316.0.21.2.5)]
[gw1] Python 3.8.9 (default, May 17 2022, 12:55:41)  -- [Clang 13.1.6 (clang-1316.0.21.2.5)]
[gw2] Python 3.8.9 (default, May 17 2022, 12:55:41)  -- [Clang 13.1.6 (clang-1316.0.21.2.5)]
[gw3] Python 3.8.9 (default, May 17 2022, 12:55:41)  -- [Clang 13.1.6 (clang-1316.0.21.2.5)]
[gw4] Python 3.8.9 (default, May 17 2022, 12:55:41)  -- [Clang 13.1.6 (clang-1316.0.21.2.5)]
[gw5] Python 3.8.9 (default, May 17 2022, 12:55:41)  -- [Clang 13.1.6 (clang-1316.0.21.2.5)]
[gw6] Python 3.8.9 (default, May 17 2022, 12:55:41)  -- [Clang 13.1.6 (clang-1316.0.21.2.5)]
[gw7] Python 3.8.9 (default, May 17 2022, 12:55:41)  -- [Clang 13.1.6 (clang-1316.0.21.2.5)]
gw0 [8] / gw1 [8] / gw2 [8] / gw3 [8] / gw4 [8] / gw5 [8] / gw6 [8] / gw7 [8]
scheduling tests via LoadFileScheduling

test/xdist_test.py::TestXdist::test_one 
test2/xdist_dummy_test.py::TestXdist::test_dummy_one 
[gw1] [ 12%] PASSED test/xdist_test.py::TestXdist::test_one 
[gw0] [ 25%] PASSED test2/xdist_dummy_test.py::TestXdist::test_dummy_one 
test/xdist_test.py::TestXdist::test_two 
test2/xdist_dummy_test.py::TestXdist::test_dummy_two 
[gw1] [ 37%] PASSED test/xdist_test.py::TestXdist::test_two 
[gw0] [ 50%] PASSED test2/xdist_dummy_test.py::TestXdist::test_dummy_two 
test2/xdist_dummy_test.py::TestXdist::test_dummy_three 
test/xdist_test.py::TestXdist::test_three 
[gw1] [ 62%] PASSED test/xdist_test.py::TestXdist::test_three 
[gw0] [ 75%] PASSED test2/xdist_dummy_test.py::TestXdist::test_dummy_three 
test2/xdist_dummy_test.py::TestXdist::test_dummy_four 
test/xdist_test.py::TestXdist::test_four 
[gw1] [ 87%] PASSED test/xdist_test.py::TestXdist::test_four 
[gw0] [100%] PASSED test2/xdist_dummy_test.py::TestXdist::test_dummy_four 

============================================================================ 8 passed in 0.38s ============================================================================

(venv) ➜  pytest pytest -n auto --dist loadgroup
=========================================================================== test session starts ===========================================================================
platform darwin -- Python 3.8.9, pytest-7.1.2, pluggy-1.0.0
rootdir: /Users/spock/PycharmProjects/pytest, configfile: pytest.ini
plugins: xdist-2.5.0, forked-1.4.0, ordering-0.6
[gw0] Python 3.8.9 (default, May 17 2022, 12:55:41)  -- [Clang 13.1.6 (clang-1316.0.21.2.5)]
[gw1] Python 3.8.9 (default, May 17 2022, 12:55:41)  -- [Clang 13.1.6 (clang-1316.0.21.2.5)]
[gw2] Python 3.8.9 (default, May 17 2022, 12:55:41)  -- [Clang 13.1.6 (clang-1316.0.21.2.5)]
[gw3] Python 3.8.9 (default, May 17 2022, 12:55:41)  -- [Clang 13.1.6 (clang-1316.0.21.2.5)]
[gw4] Python 3.8.9 (default, May 17 2022, 12:55:41)  -- [Clang 13.1.6 (clang-1316.0.21.2.5)]
[gw5] Python 3.8.9 (default, May 17 2022, 12:55:41)  -- [Clang 13.1.6 (clang-1316.0.21.2.5)]
[gw6] Python 3.8.9 (default, May 17 2022, 12:55:41)  -- [Clang 13.1.6 (clang-1316.0.21.2.5)]
[gw7] Python 3.8.9 (default, May 17 2022, 12:55:41)  -- [Clang 13.1.6 (clang-1316.0.21.2.5)]
gw0 [8] / gw1 [8] / gw2 [8] / gw3 [8] / gw4 [8] / gw5 [8] / gw6 [8] / gw7 [8]
scheduling tests via LoadGroupScheduling

test2/xdist_dummy_test.py::TestXdist::test_dummy_three@group2 
test2/xdist_dummy_test.py::TestXdist::test_dummy_one@group1 
[gw1] [ 12%] PASSED test2/xdist_dummy_test.py::TestXdist::test_dummy_three@group2 
test2/xdist_dummy_test.py::TestXdist::test_dummy_four@group2 
[gw0] [ 25%] PASSED test2/xdist_dummy_test.py::TestXdist::test_dummy_one@group1 
[gw1] [ 37%] PASSED test2/xdist_dummy_test.py::TestXdist::test_dummy_four@group2 
test2/xdist_dummy_test.py::TestXdist::test_dummy_two@group1 
[gw0] [ 50%] PASSED test2/xdist_dummy_test.py::TestXdist::test_dummy_two@group1 
test/xdist_test.py::TestXdist::test_three@group2 
[gw1] [ 62%] PASSED test/xdist_test.py::TestXdist::test_three@group2 
test/xdist_test.py::TestXdist::test_four@group2 
[gw1] [ 75%] PASSED test/xdist_test.py::TestXdist::test_four@group2 
test/xdist_test.py::TestXdist::test_one@group1 
[gw0] [ 87%] PASSED test/xdist_test.py::TestXdist::test_one@group1 
test/xdist_test.py::TestXdist::test_two@group1 
[gw0] [100%] PASSED test/xdist_test.py::TestXdist::test_two@group1 

============================================================================ 8 passed in 0.40s ============================================================================

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 盒模型 點擊打開視頻教程 標準盒模型、怪異盒模型(IE盒模型) 什麼是盒模型? 盒模型的作用:規定了網頁元素如何顯示以及元素間的相互關係 盒模型的概念:盒模型是css佈局的基石,它規定了網頁元素如何顯示以及元素間的相互關係。 css定義所有的元素都可以擁有像盒子一樣的外形和平面空間。即都包含內容區、 ...
  • 背景 項目中用到了vue的element-ui框架,用到了el-tree組件。由於數據量很大,使用了數據懶載入模式,即非同步樹。非同步樹採用覆選框進行結點選擇的時候,沒法自動展開,官方文檔找了半天也沒有找到好的辦法! 找不到相關的配置,或者方法可以使用。 經過調試與閱讀elment-ui源碼才發現有現成 ...
  • 有時候我們不希望組件被重新渲染影響使用體驗;或者處於性能考慮,避免多次重覆渲染降低性能。而是希望組件可以緩存下來,維持當前的狀態。這時候就需要用到keep-alive組件。 開啟keep-alive 生命周期的變化. 初次進入時: onMounted> onActivated 退出後觸發 deact ...
  • 本文是深入淺出 ahooks 源碼系列文章的第三篇,該系列已整理成文檔-地址。覺得還不錯,給個 star 支持一下哈,Thanks。 本文來探索一下 ahooks 是怎麼解決 React 的閉包問題的?。 React 的閉包問題 先來看一個例子: import React, { useState, ...
  • 介紹 es表示ECMASCript ,他是從es3,es5,es6,es5是2009.12月發佈的,es6是2015.6月發佈的。vue2完全支持es5的(vue3完全支持es6的),react完全支持es6 es5的新特性 嚴格模式(對應的相反的稱為怪異模式) 'use strict' //一般用 ...
  • useRequest 是 ahooks 最核心的功能之一,它的功能非常豐富,但核心代碼(Fetch 類)相對簡單,這得益於它的插件化機制 ...
  • 作為系列的第一篇,介紹了 React hooks utils 庫的背景以及 ahooks 的特點簡介和整體結構,接下來會探索各個常見的 hooks 方法實現,敬請期待。 ...
  • Vue.use()的作用及原理 點擊打開視頻講解 在Vue中引入使用第三方庫通常我們都會採用import的形式引入進來 但是有的組件在引入之後又做了Vue.use()操作 有的組件引入進來又進行了Vue.prototype.$axios = axios 那麼它們之間有什麼聯繫呢? 例如:Vue.us ...
一周排行
    -Advertisement-
    Play Games
  • 一、openKylin簡介 openKylin(開放麒麟) 社區是在開源、自願、平等和協作的基礎上,由基礎軟硬體企業、非營利性組織、社團組織、高等院校、科研機構和個人開發者共同創立的一個開源社區,致力於通過開源、開放的社區合作,構建桌面操作系統開源社區,推動Linux開源技術及其軟硬體生態繁榮發展。 ...
  • 簡介 Flurl是一個用於構建基於HTTP請求的C#代碼的庫。它的主要目的是簡化和優雅地處理網路請求(只用很少的代碼完成請求)。Flurl提供了一種簡單的方法來構建GET、POST、PUT等類型的請求,以及處理響應和異常。它還提供了一些高級功能,如鏈式調用、緩存請求結果、自動重定向等。本文將介紹Fl ...
  • 一:背景 1. 講故事 最近也挺奇怪,看到了兩起 CPU 爆高的案例,且誘因也是一致的,覺得有一些代表性,合併分享出來幫助大家來避坑吧,閑話不多說,直接上 windbg 分析。 二:WinDbg 分析 1. CPU 真的爆高嗎 這裡要提醒一下,別人說爆高不一定真的就是爆高,我們一定要拿數據說話,可以 ...
  • 剛開始寫文章,封裝Base基類的時候,添加了trycatch異常塊,不過當時沒有去記錄日誌,直接return了。有小伙伴勸我不要吃了Exception 其實沒有啦,項目剛開始,我覺得先做好整體結構比較好。像是蓋樓一樣。先把樓體建造出來,然後再一步一步的美化完善。 基礎的倉儲模式已經ok,Autofa ...
  • 框架目標 什麼是框架,框架能做到什麼? 把一個方向的技術研發做封裝,具備通用性,讓使用框架的開發者用起來很輕鬆。 屬性: 通用性 健壯性 穩定性 擴展性 高性能 組件化 跨平臺 從零開始-搭建框架 建立項目 主鍵查詢功能開發 綁定實體 一步一步的給大家推導: 一邊寫一邊測試 從零開始--搭建框架 1 ...
  • 大家好,我是沙漠盡頭的狼。 本方首發於Dotnet9,介紹使用dnSpy調試第三方.NET庫源碼,行文目錄: 安裝dnSpy 編寫示常式序 調試示常式序 調試.NET庫原生方法 總結 1. 安裝dnSpy dnSpy是一款功能強大的.NET程式反編譯工具,可以對.NET程式進行反編譯,代替庫文檔的功 ...
  • 在`Windows`操作系統中,每個進程的虛擬地址空間都被劃分為若幹記憶體塊,每個記憶體塊都具有一些屬性,如記憶體大小、保護模式、類型等。這些屬性可以通過`VirtualQueryEx`函數查詢得到。該函數可用於查詢進程虛擬地址空間中的記憶體信息的函數。它的作用類似於`Windows`操作系統中的`Task... ...
  • 背景介紹 1,最近有一個大數據量插入的操作入庫的業務場景,需要先做一些其他修改操作,然後在執行插入操作,由於插入數據可能會很多,用到多線程去拆分數據並行處理來提高響應時間,如果有一個線程執行失敗,則全部回滾。 2,在spring中可以使用@Transactional註解去控制事務,使出現異常時會進行 ...
  • 線程(thread)是操作系統能夠進行運算調度的最小單位。它被包含在進程之中,是進程中的實際 運作單位。一條線程指的是進程中一個單一順序的控制流,一個進程中可以併發多個線程,每條線 程並行執行不同的任務。 ...
  • 發現Java 21的StringBuilder和StringBuffer中多了repeat方法: /** * @throws IllegalArgumentException {@inheritDoc} * * @since 21 */ @Override public StringBuilder ...