vault backup: 2025-01-22 15:16:48

This commit is contained in:
deng 2025-01-22 15:16:48 +08:00
parent 107dcb6da8
commit 724d9f2f15
3 changed files with 78 additions and 10 deletions

View File

@ -20,8 +20,23 @@
"icon": "lucide-file",
"title": "{Note} Poetry"
}
},
{
"id": "da4efdc0e3adde70",
"type": "leaf",
"state": {
"type": "markdown",
"state": {
"file": "組織/EverfortuneAI/{Note} Pytest.md",
"mode": "source",
"source": true
},
"icon": "lucide-file",
"title": "{Note} Pytest"
}
}
]
],
"currentTab": 1
},
{
"id": "97c43f9093922695",
@ -40,8 +55,23 @@
"icon": "lucide-file",
"title": "{Note} Poetry"
}
},
{
"id": "60971798df1781bf",
"type": "leaf",
"state": {
"type": "markdown",
"state": {
"file": "組織/EverfortuneAI/{Note} Pytest.md",
"mode": "source",
"source": false
},
"icon": "lucide-file",
"title": "{Note} Pytest"
}
}
]
],
"currentTab": 1
}
],
"direction": "vertical"
@ -113,7 +143,7 @@
"state": {
"type": "backlink",
"state": {
"file": "組織/EverfortuneAI/{Note} Poetry.md",
"file": "組織/EverfortuneAI/{Note} Pytest.md",
"collapseAll": false,
"extraContext": false,
"sortOrder": "alphabetical",
@ -123,7 +153,7 @@
"unlinkedCollapsed": true
},
"icon": "links-coming-in",
"title": "{Note} Poetry 的反向連結"
"title": "{Note} Pytest 的反向連結"
}
},
{
@ -132,12 +162,12 @@
"state": {
"type": "outgoing-link",
"state": {
"file": "組織/EverfortuneAI/{Note} Poetry.md",
"file": "組織/EverfortuneAI/{Note} Pytest.md",
"linksCollapsed": false,
"unlinkedCollapsed": true
},
"icon": "links-going-out",
"title": "{Note} Poetry 的對外連結"
"title": "{Note} Pytest 的對外連結"
}
},
{
@ -159,10 +189,10 @@
"state": {
"type": "outline",
"state": {
"file": "組織/EverfortuneAI/{Note} Poetry.md"
"file": "組織/EverfortuneAI/{Note} Pytest.md"
},
"icon": "lucide-list",
"title": "{Note} Poetry 的大綱"
"title": "{Note} Pytest 的大綱"
}
},
{
@ -193,10 +223,12 @@
"obsidian-git:Open Git source control": false
}
},
"active": "23dfe70ddddd299f",
"active": "da4efdc0e3adde70",
"lastOpenFiles": [
"組織/EverfortuneAI/{Note} Nox.md",
"組織/EverfortuneAI/{Note} Poetry.md",
"組織/EverfortuneAI/{Note} Pytest.md",
"組織/EverfortuneAI/resources/test_your_code.png",
"組織/EverfortuneAI/{Note} Nox.md",
"組織/EverfortuneAI/{Note} Packaging Python Project.md",
"組織/EverfortuneAI/resources/poetry.png",
"README.md",

Binary file not shown.

After

Width:  |  Height:  |  Size: 576 KiB

View File

@ -0,0 +1,36 @@
---
author: deng
date: "20250122"
category: Note
tags:
- Python
- UnitTest
- Pytest
- Note
---
![cover](resources/test_your_code.png)
- Abstract
- [Pytest](https://docs.pytest.org/en/stable/) is a popular testing framework for Python known for its simplicity. It breaks away from the class-based structure, offering a more flexible and intuitive approach. (Yep. More Pythonic than [unittest](https://docs.python.org/3/library/unittest.html)!)
- Resources
- 心法
- [為什麼你應該寫單元測試——《Python 工匠》筆記 ](https://blog.kyomind.tw/python-craftsman-02/) - Code and Me
- 入門
- [Python 測試入門 - 開始使用 PyTest](https://blog.tzing.tw/posts/python-testing-pytest-08de903a) - 拾遺
- [# Python 測試入門 - PyTest Fixture](https://blog.tzing.tw/posts/python-testing-pytest-fixture-91b547f2) - 拾遺
- [7 個實用的 pytest plugins](https://myapollo.com.tw/blog/7-useful-pytest-plugins/) - MyApollo
- [Pytest 101 - 給 Python 開發者的測試入門 (3) - 圖解 Mock & 測試框架語法整理](https://minglunwu.com/notes/2023/pytest_101_3.html/) - Byte and Ink
- [How To Test Python Exception Handling Using Pytest Assert (A Simple Guide)](https://pytest-with-eric.com/introduction/pytest-assert-exception/) - Pytest with Eric
- 更上一層樓
- [13 Proven Ways To Improve Test Runtime With Pytest](https://pytest-with-eric.com/pytest-advanced/pytest-improve-runtime/) - Pytest with Eric
- 對於進階者相當推薦Eric的[部落格](https://pytest-with-eric.com/),有相當多的情境可以參考
- Note
- 雖不追求100%測試覆蓋率,但我在寫測試時都會裝上[Pytest-cov](https://pytest-cov.readthedocs.io/en/latest/readme.html)插件以掌握專案當下覆蓋率的情況
- 透過``--cov``引數調用,例如``pytest --cov=./src ./tests``
- 專案越肥大通常測試會花上更多時間,搭配[Pytest-xdist](https://pytest-xdist.readthedocs.io/en/stable/distribution.html)插件可簡單地將測試平行化以節省寶貴時間
- 透過``-n``引數調用,例如``pytest -n auto``
- 善用pytest namespace可以將某些資料暫存給後續測試使用進一步節省測試時間
- 若測試物件之間有依賴性可使用xdist_group_mark將物件分群同群的物件將會被同一行程執行
- 平行化通常伴隨更多資源的消耗,使用時需小心~
- Pytest也支援[pyproject.toml](https://docs.pytest.org/en/stable/reference/customize.html#pyproject-toml)進行配置