obsidian_love/組織/EverfortuneAI/{Note} Nox.md

49 lines
1.9 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
author: deng
date: "20250109"
category: Note
tags:
- Python
- Nox
- FlexibleTest
- Versioning
---
![cover](resources/nox.png)
[image resource](https://nox.thea.codes/en/stable/index.html)
- Abstract
- 在將Python專案打包時會遇到填寫依賴套件及其版本的問題開發者必須逐一測試Python版本與依賴套件版本之間甚至是跨平台不同組合的可運行性而Nox可以將這一流程自動化
- Steps
1. 在開發環境中安裝Nox`python -m pip install nox`
2. 在Package資料夾中產生*noxfile.py* 並撰寫測試程式碼範例程式碼可見Note
3. 於終端機輸入`nox` 開始進行測試
- Note
- 若只想執行某個函式的程式碼,可執行`nox -s [function_name]`
- 若想將測試結果輸出檔案(測試組合很多的時候很適合),可執行`nox --report [file_name].json`
- 小知識如果semantic version沒有給patch的話則其為0比方說我們要測試的numpy版本設為1.24則其實際測試的版本為1.24.0
- 測試的環境會放在`.nox`裡通常以python搭配套件名稱進行資料夾命名若套件太多則會hash成短名稱如果使用`reuse_venv`則nox會去重新使用這些環境檔進行測試可節省重新建立環境的時間
- 以下為*noxfile.py*範例程式碼(透過環境是否能順利通過單元測試來判別套件之間的匹配性)
```python
# noxfile.py
#
# author: deng
# date : 20250110
import nox
@nox.session(venv_backend='conda', python=['3.10', '3.11', '3.12'])
@nox.parametrize('numpy', ['1.26', '2.0'])
@nox.parametrize('scikit_learn', ['1.5', '1.6'])
def test_flexibility(session, numpy, scikit_learn):
# Install package dependencies
session.install(f'numpy=={numpy}')
session.install(f'scikit-learn=={scikit_learn}')
# Test
session.install('pytest')
session.run('pytest', 'tests', '-v')
```