👷 use pytest-xdist to accelerate test (#186)

* 👷 use pytest-xdist to accelerate test

*  mark flaky

* test(mcbbsnews): 删除重复测试,为测试添加flaky

---------

Co-authored-by: hemengyang <hmy0119@gmail.com>
Co-authored-by: AzideCupric <rukuy@qq.com>
This commit is contained in:
felinae98 2023-03-03 17:16:15 +08:00 committed by GitHub
parent 0201d5b316
commit c3a764581e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 78 additions and 19339 deletions

View File

@ -75,7 +75,7 @@ jobs:
run: poetry install
- name: Run Pytest
run: poetry run pytest --cov-report xml --cov=./src/plugins/nonebot_bison -k 'not compare and not render'
run: poetry run pytest --cov-report xml --cov=./src/plugins/nonebot_bison -k 'not compare and not render' -n auto
- name: Upload coverage report
uses: codecov/codecov-action@v3
@ -108,7 +108,7 @@ jobs:
run: poetry install
- name: Run Pytest
run: poetry run pytest --cov-report xml --cov=./src/plugins/nonebot_bison -k 'not compare'
run: poetry run pytest --cov-report xml --cov=./src/plugins/nonebot_bison -k 'not compare' -n auto
- name: Upload coverage report
uses: codecov/codecov-action@v3

66
poetry.lock generated
View File

@ -525,6 +525,21 @@ files = [
[package.extras]
test = ["pytest (>=6)"]
[[package]]
name = "execnet"
version = "1.9.0"
description = "execnet: rapid multi-Python deployment"
category = "dev"
optional = false
python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*"
files = [
{file = "execnet-1.9.0-py2.py3-none-any.whl", hash = "sha256:a295f7cc774947aac58dde7fdc85f4aa00c42adf5d8f5468fc630c1acf30a142"},
{file = "execnet-1.9.0.tar.gz", hash = "sha256:8f694f3ba9cc92cab508b152dcfe322153975c29bda272e2fd7f3f00f36e47c5"},
]
[package.extras]
testing = ["pre-commit"]
[[package]]
name = "executing"
version = "1.2.0"
@ -1738,6 +1753,33 @@ files = [
[package.dependencies]
wcwidth = "*"
[[package]]
name = "psutil"
version = "5.9.4"
description = "Cross-platform lib for process and system monitoring in Python."
category = "dev"
optional = false
python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*"
files = [
{file = "psutil-5.9.4-cp27-cp27m-macosx_10_9_x86_64.whl", hash = "sha256:c1ca331af862803a42677c120aff8a814a804e09832f166f226bfd22b56feee8"},
{file = "psutil-5.9.4-cp27-cp27m-manylinux2010_i686.whl", hash = "sha256:68908971daf802203f3d37e78d3f8831b6d1014864d7a85937941bb35f09aefe"},
{file = "psutil-5.9.4-cp27-cp27m-manylinux2010_x86_64.whl", hash = "sha256:3ff89f9b835100a825b14c2808a106b6fdcc4b15483141482a12c725e7f78549"},
{file = "psutil-5.9.4-cp27-cp27m-win32.whl", hash = "sha256:852dd5d9f8a47169fe62fd4a971aa07859476c2ba22c2254d4a1baa4e10b95ad"},
{file = "psutil-5.9.4-cp27-cp27m-win_amd64.whl", hash = "sha256:9120cd39dca5c5e1c54b59a41d205023d436799b1c8c4d3ff71af18535728e94"},
{file = "psutil-5.9.4-cp27-cp27mu-manylinux2010_i686.whl", hash = "sha256:6b92c532979bafc2df23ddc785ed116fced1f492ad90a6830cf24f4d1ea27d24"},
{file = "psutil-5.9.4-cp27-cp27mu-manylinux2010_x86_64.whl", hash = "sha256:efeae04f9516907be44904cc7ce08defb6b665128992a56957abc9b61dca94b7"},
{file = "psutil-5.9.4-cp36-abi3-macosx_10_9_x86_64.whl", hash = "sha256:54d5b184728298f2ca8567bf83c422b706200bcbbfafdc06718264f9393cfeb7"},
{file = "psutil-5.9.4-cp36-abi3-manylinux_2_12_i686.manylinux2010_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:16653106f3b59386ffe10e0bad3bb6299e169d5327d3f187614b1cb8f24cf2e1"},
{file = "psutil-5.9.4-cp36-abi3-manylinux_2_12_x86_64.manylinux2010_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:54c0d3d8e0078b7666984e11b12b88af2db11d11249a8ac8920dd5ef68a66e08"},
{file = "psutil-5.9.4-cp36-abi3-win32.whl", hash = "sha256:149555f59a69b33f056ba1c4eb22bb7bf24332ce631c44a319cec09f876aaeff"},
{file = "psutil-5.9.4-cp36-abi3-win_amd64.whl", hash = "sha256:fd8522436a6ada7b4aad6638662966de0d61d241cb821239b2ae7013d41a43d4"},
{file = "psutil-5.9.4-cp38-abi3-macosx_11_0_arm64.whl", hash = "sha256:6001c809253a29599bc0dfd5179d9f8a5779f9dffea1da0f13c53ee568115e1e"},
{file = "psutil-5.9.4.tar.gz", hash = "sha256:3d7f9739eb435d4b1338944abe23f49584bde5395f27487d2ee25ad9a8774a62"},
]
[package.extras]
test = ["enum34", "ipaddress", "mock", "pywin32", "wmi"]
[[package]]
name = "ptyprocess"
version = "0.7.0"
@ -1974,6 +2016,28 @@ pytest = ">=5.0"
[package.extras]
dev = ["pre-commit", "pytest-asyncio", "tox"]
[[package]]
name = "pytest-xdist"
version = "3.2.0"
description = "pytest xdist plugin for distributed testing, most importantly across multiple CPUs"
category = "dev"
optional = false
python-versions = ">=3.7"
files = [
{file = "pytest-xdist-3.2.0.tar.gz", hash = "sha256:fa10f95a2564cd91652f2d132725183c3b590d9fdcdec09d3677386ecf4c1ce9"},
{file = "pytest_xdist-3.2.0-py3-none-any.whl", hash = "sha256:336098e3bbd8193276867cc87db8b22903c3927665dff9d1ac8684c02f597b68"},
]
[package.dependencies]
execnet = ">=1.1"
psutil = {version = ">=3.0", optional = true, markers = "extra == \"psutil\""}
pytest = ">=6.2.0"
[package.extras]
psutil = ["psutil (>=3.0)"]
setproctitle = ["setproctitle"]
testing = ["filelock"]
[[package]]
name = "python-dotenv"
version = "1.0.0"
@ -2830,4 +2894,4 @@ multidict = ">=4.0"
[metadata]
lock-version = "2.0"
python-versions = ">=3.10,<4.0.0"
content-hash = "1235a5dd8c759eeff697bcc5572ac8b83a3882636fafa943a3274914a7041060"
content-hash = "8fa5478ec628b0baab07d2a745e7cbd6d9c54689ecb330a533819b4455f3a499"

View File

@ -50,6 +50,7 @@ sqlalchemy-stubs = "^0.4"
nonebot2 = { extras = ["fastapi"], version = ">=2.0.0-rc.2" }
pytest-mock = "^3.10.0"
nonebug = { git = "https://github.com/nonebot/nonebug.git", rev = "master" }
pytest-xdist = { extras = ["psutil"], version = "^3.1.0" }
[build-system]
requires = ["poetry>=0.12"]

View File

@ -1,218 +0,0 @@
[
{
"url": "thread-1340080-1-1.html",
"title": "Mojang Status服务器出现一些小问题",
"category": "块讯",
"author": "DreamVoid",
"id": "normalthread_1340080",
"date": 1652630400
},
{
"url": "thread-1339940-1-1.html",
"title": "kinbdogz 就近期荒野更新的风波发表看法",
"category": "块讯",
"author": "卡狗",
"id": "normalthread_1339940",
"date": 1652630400
},
{
"url": "thread-1339097-1-1.html",
"title": "Minecraft 基岩版 1.18.33 发布(仅 Switch",
"category": "基岩版资讯",
"author": "电量量",
"id": "normalthread_1339097",
"date": 1652457600
},
{
"url": "thread-1338607-1-1.html",
"title": "Minecraft Java版 22w19a 发布",
"category": "Java版资讯",
"author": "寂华",
"id": "normalthread_1338607",
"date": 1652371200
},
{
"url": "thread-1338592-1-1.html",
"title": "Minecraft 基岩版 Beta & Preview 1.19.0.32/33 发布",
"category": "基岩版资讯",
"author": "苦力怕553",
"id": "normalthread_1338592",
"date": 1652371200
},
{
"url": "thread-1338588-1-1.html",
"title": "请给我们一个真正的“荒野更新”",
"category": "时评",
"author": "斯乌",
"id": "normalthread_1338588",
"date": 1652371200
},
{
"url": "thread-1338496-1-1.html",
"title": "slicedlime周三无快照推迟至周四",
"category": "块讯",
"author": "橄榄Chan",
"id": "normalthread_1338496",
"date": 1652198400
},
{
"url": "thread-1336371-1-1.html",
"title": "Minecraft 基岩版 1.18.32 发布(仅 Android、NS【新增 NS 平台】",
"category": "基岩版资讯",
"author": "电量量",
"id": "normalthread_1336371",
"date": 1651766400
},
{
"url": "thread-1335897-1-1.html",
"title": "Minecraft 基岩版 Beta & Preview 1.19.0.30/31 发布",
"category": "基岩版资讯",
"author": "AzureZeng",
"id": "normalthread_1335897",
"date": 1651680000
},
{
"url": "thread-1335891-1-1.html",
"title": "Minecraft Java版 22w18a 发布",
"category": "Java版资讯",
"author": "Aurora_Feather",
"id": "normalthread_1335891",
"date": 1651680000
},
{
"url": "thread-1333196-1-1.html",
"title": "Minecraft 基岩版 Beta & Preview 1.19.0.28/29 发布",
"category": "基岩版资讯",
"author": "希铁石z",
"id": "normalthread_1333196",
"date": 1651161600
},
{
"url": "thread-1332834-1-1.html",
"title": "Minecraft 基岩版 1.18.31 发布",
"category": "基岩版资讯",
"author": "希铁石z",
"id": "normalthread_1332834",
"date": 1651075200
},
{
"url": "thread-1332811-1-1.html",
"title": "Minecraft Java版 22w17a 发布",
"category": "Java版资讯",
"author": "卡狗",
"id": "normalthread_1332811",
"date": 1651075200
},
{
"url": "thread-1332424-1-1.html",
"title": "Mojang Status正在寻找1.18.30更新问题的解决方案",
"category": "基岩块讯",
"author": "ArmorRush",
"id": "normalthread_1332424",
"date": 1650988800
},
{
"url": "thread-1329712-1-1.html",
"title": "Minecraft 基岩版 Beta & Preview 1.19.0.26/27 发布",
"category": "基岩版资讯",
"author": "ArmorRush",
"id": "normalthread_1329712",
"date": 1650470400
},
{
"url": "thread-1329651-1-1.html",
"title": "Minecraft Java版 22w16b 发布",
"category": "Java版资讯",
"author": "卡狗",
"id": "normalthread_1329651",
"date": 1650470400
},
{
"url": "thread-1329644-1-1.html",
"title": "Minecraft Java版 22w16a 发布",
"category": "Java版资讯",
"author": "希铁石z",
"id": "normalthread_1329644",
"date": 1650470400
},
{
"url": "thread-1329335-1-1.html",
"title": "Minecraft 基岩版 1.18.30 发布",
"category": "基岩版资讯",
"author": "ArmorRush",
"id": "normalthread_1329335",
"date": 1650384000
},
{
"url": "thread-1328892-1-1.html",
"title": "“海王” 杰森·莫玛 有望主演《我的世界》大电影",
"category": "块讯",
"author": "广药",
"id": "normalthread_1328892",
"date": 1650297600
},
{
"url": "thread-1327089-1-1.html",
"title": "Minecraft 基岩版 Beta & Preview 1.19.0.24/25 发布",
"category": "基岩版资讯",
"author": "ArmorRush",
"id": "normalthread_1327089",
"date": 1649952000
},
{
"url": "thread-1326640-1-1.html",
"title": "Minecraft Java版 22w15a 发布",
"category": "Java版资讯",
"author": "ArmorRush",
"id": "normalthread_1326640",
"date": 1649865600
},
{
"url": "thread-1323762-1-1.html",
"title": "Minecraft 基岩版 Beta & Preview 1.19.0.20 发布",
"category": "基岩版资讯",
"author": "ArmorRush",
"id": "normalthread_1323762",
"date": 1649260800
},
{
"url": "thread-1323662-1-1.html",
"title": "Minecraft Java版 22w14a 发布",
"category": "Java版资讯",
"author": "卡狗",
"id": "normalthread_1323662",
"date": 1649260800
},
{
"url": "thread-1321419-1-1.html",
"title": "[愚人节] Minecraft Java版 22w13oneBlockAtATime 发布",
"category": "Java版资讯",
"author": "希铁石z",
"id": "normalthread_1321419",
"date": 1648742400
},
{
"url": "thread-1320986-1-1.html",
"title": "Minecraft近期没有为主机平台添加光线追踪的计划",
"category": "基岩块讯",
"author": "ArmorRush",
"id": "normalthread_1320986",
"date": 1648742400
},
{
"url": "thread-1320931-1-1.html",
"title": "Minecraft Java版 22w13a 发布",
"category": "Java版资讯",
"author": "卡狗",
"id": "normalthread_1320931",
"date": 1648742400
},
{
"url": "thread-1342236-1-1.html",
"title": "Minecraft: 加入Microsoft Rewards赢取限量Xbox Series S",
"category": "周边",
"author": "ETW_Derp",
"id": "normalthread_1342236",
"date": 1648742400
}
]

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -1,5 +1,6 @@
import pytest
import respx
from flaky import flaky
from httpx import AsyncClient, Response
from nonebug.app import App
@ -16,93 +17,14 @@ def mcbbsnews(app: App):
@pytest.fixture(scope="module")
def raw_post_list():
return get_json("mcbbsnews/mcbbsnews_raw_post_list.json")
return get_json("mcbbsnews/mcbbsnews_raw_post_list_update.json")
@pytest.mark.asyncio
@pytest.mark.render
@respx.mock
async def test_javanews_parser(mcbbsnews, raw_post_list):
javanews_mock = respx.get("https://www.mcbbs.net/thread-1338607-1-1.html")
javanews_mock.mock(
return_value=Response(
200, text=get_file("mcbbsnews/mock/mcbbsnews_javanews.html")
)
)
raw_post = raw_post_list[3]
post = await mcbbsnews.parse(raw_post)
assert post.text == "{}\n\n└由 {} 发表".format(raw_post["title"], raw_post["author"])
assert post.target_name == raw_post["category"]
assert len(post.pics) == 1
@pytest.mark.asyncio
@pytest.mark.render
@respx.mock
async def test_bedrocknews_parser(mcbbsnews, raw_post_list):
bedrocknews_mock = respx.get("https://www.mcbbs.net/thread-1338592-1-1.html")
bedrocknews_mock.mock(
return_value=Response(
200, text=get_file("mcbbsnews/mock/mcbbsnews_bedrocknews.html")
)
)
raw_post = raw_post_list[4]
post = await mcbbsnews.parse(raw_post)
assert post.text == "{}\n\n└由 {} 发表".format(raw_post["title"], raw_post["author"])
assert post.target_name == raw_post["category"]
assert len(post.pics) == 1
@pytest.mark.asyncio
@pytest.mark.render
@respx.mock
async def test_bedrock_express_parser(mcbbsnews, raw_post_list):
bedrock_express_mock = respx.get("https://www.mcbbs.net/thread-1332424-1-1.html")
bedrock_express_mock.mock(
return_value=Response(
200, text=get_file("mcbbsnews/mock/mcbbsnews_bedrock_express.html")
)
)
raw_post = raw_post_list[13]
post = await mcbbsnews.parse(raw_post)
assert post.target_name == raw_post["category"]
assert post.text == "{}\n\n└由 {} 发表".format(raw_post["title"], raw_post["author"])
@pytest.mark.asyncio
@pytest.mark.render
@respx.mock
async def test_java_express_parser(mcbbsnews, raw_post_list):
java_express_mock = respx.get("https://www.mcbbs.net/thread-1340080-1-1.html")
java_express_mock.mock(
return_value=Response(
200, text=get_file("mcbbsnews/mock/mcbbsnews_java_express.html")
)
)
raw_post = raw_post_list[0]
post = await mcbbsnews.parse(raw_post)
assert post.target_name == raw_post["category"]
assert post.text == "{}\n\n└由 {} 发表".format(raw_post["title"], raw_post["author"])
@pytest.mark.asyncio
@pytest.mark.render
@respx.mock
async def test_merch_parser(mcbbsnews, raw_post_list):
mc_merch_mock = respx.get("https://www.mcbbs.net/thread-1342236-1-1.html")
mc_merch_mock.mock(
return_value=Response(200, text=get_file("mcbbsnews/mock/mcbbsnews_merch.html"))
)
raw_post = raw_post_list[26]
post = await mcbbsnews.parse(raw_post_list[26])
assert post.target_name == raw_post["category"]
assert post.text == "{}\n\n└由 {} 发表".format(raw_post["title"], raw_post["author"])
@pytest.mark.asyncio
@pytest.mark.render
@respx.mock
async def test_fetch_new(mcbbsnews, dummy_user_subinfo):
@flaky(max_runs=3, min_passes=1)
async def test_fetch_new(mcbbsnews, dummy_user_subinfo, raw_post_list):
news_router = respx.get("https://www.mcbbs.net/forum-news-1.html")
news_router.mock(
return_value=Response(
@ -127,15 +49,18 @@ async def test_fetch_new(mcbbsnews, dummy_user_subinfo):
res = await mcbbsnews.fetch_new_post(target, [dummy_user_subinfo])
assert news_router.called
post = res[0][1][0]
raw_post = raw_post_list[0]
assert post.target_type == "MCBBS幻翼块讯"
assert post.text == "Minecraft Java版 1.19-pre1 发布\n\n└由 希铁石z 发表"
assert post.url == "https://www.mcbbs.net/thread-1340927-1-1.html"
assert post.target_name == "Java版资讯"
assert post.text == "{}\n\n└由 {} 发表".format(raw_post["title"], raw_post["author"])
assert post.url == "https://www.mcbbs.net/{}".format(raw_post["url"])
assert post.target_name == raw_post["category"]
assert len(post.pics) == 1
@pytest.mark.asyncio
@pytest.mark.render
@respx.mock
@flaky(max_runs=3, min_passes=1)
async def test_news_render(mcbbsnews, dummy_user_subinfo):
new_post = respx.get("https://www.mcbbs.net/thread-1340927-1-1.html")
new_post.mock(