英文| nox tutorial
出處| nox 官方文檔
譯者| 豌豆花下貓@Python貓
Github地址:https://github.com/chinesehuazhou/nox_doc_cn
聲明:本翻譯基於CC BY-NC-SA 4.0授權協議,內容略有改動,轉載請保留原文出處,請勿用於商業或非法用途。
本教程將引導你學會安裝、配置和運行 Nox。
安裝
Nox 可以通過pip輕松安裝:
python3 -m pip install nox
你可能希望使用用戶站點(user site)來避免對全局的 Python install 造成混亂:
python3 -m pip install --user nox
或者,你也可以更精致,使用pipx:
pipx install nox
無論用哪種方式,Nox 通常是要全局安裝的,類似於 tox、pip和其它類似的工具。
如果你有興趣在docker 內運行 nox,可以使用 DockerHub 上的thekevjames/nox鏡像,它包含所有 nox 版本的構建與及所有支持的 Python 版本。
如果你想在GitHub Actions中運行 nox ,則可以使用Activatedleigh/setup-nox action,它將安裝最新的 nox,並令 GitHub Actions 環境提供的所有 Python 版本可用。
編寫配置文件
Nox 通過項目目錄中一個名為 noxfile.py 的文件作配置 。這是一個 Python文件,定義了一組會話(sessions)。一個會話是一個環境和一組在這個環境中運行的命令。如果你熟悉 tox,會話就類似於它的環境。如果你熟悉 GNU Make,會話則類似於它的 target。
會話使用 @nox.session 裝飾器作聲明。這方式類似於 Flask 使用 @app.route。
下面是一個基本的 Nox 文件,對 example.py 運行flake8(你可以自己創建example.py):
import nox
@nox.session
def lint(session):
session.install("flake8")
session.run("flake8", "example.py")
第一次運行 Nox
現在,你已經安裝了 Nox 並擁有一個配置文件, 那就可以運行 Nox 了!在終端中打開項目的目錄,然后運行nox 。你應該會看到類似這樣的內容:
$ nox
nox > Running session lint
nox > Creating virtualenv using python3.7 in .nox/lint
nox > pip install flake8
nox > flake8 example.py
nox > Session lint was successful.
✨現在你已第一次成功地使用 Nox 啦!✨
本教程的其余部分將帶你學習其它可以用 Nox 完成的常見操作。如果需要的話,你還可以跳至命令行用法和配置&API文檔。
安裝依賴項
Nox 基本上是將 session.install 傳遞給 pip ,因此你可以用通常的方式來安裝東西。這里有一些例子:
(1)一次安裝一個或多個包:
@nox.session
def tests(session):
# same as pip install pytest protobuf>3.0.0
session.install("pytest", "protobuf>3.0.0")
...
(2)根據 requirements.txt 文件安裝:
@nox.session
def tests(session):
# same as pip install -r -requirements.txt
session.install("-r", "requirements.txt")
...
(3)如果你的項目是一個 Python 包,而你想安裝它:
@nox.session
def tests(session):
# same as pip install .
session.install(".")
...
運行命令
session.run 函數可讓你在會話的虛擬環境的上下文中運行命令。以下是一些示例:
(1)你可以安裝和運行 Python 工具:
@nox.session
def tests(session):
session.install("pytest")
session.run("pytest")
(2)如果你想給一個程序傳遞更多的參數,只需給 run 添加更多參數即可:
@nox.session
def tests(session):
session.install("pytest")
session.run("pytest", "-v", "tests")
(3)你還可以傳遞環境變量:
@nox.session
def tests(session):
session.install("black")
session.run(
"pytest",
env={
"FLASK_DEBUG": "1"
}
)
有關運行程序的更多選項和示例,請參見nox.sessions.Session.run()。
選擇要運行的會話
一旦你的 Noxfile 中有多個會話,你會注意到 Nox 將默認運行所有的會話。盡管這很有用,但是通常一次只需要運行一兩個。
你可以使用--sessions參數(或-s)來選擇要運行的會話。你可以使用--list參數顯示哪些會話可用,哪些將會運行。這里有一些例子:
這是一個具有三個會話的 Noxfile:
import nox
@nox.session
def test(session):
...
@nox.session
def lint(session):
...
@nox.session
def docs(session):
...
如果你只運行nox --list ,則會看到所有會話都被選中:
Sessions defined in noxfile.py:
* test
* lint
* docs
sessions marked with * are selected,
sessions marked with - are skipped.
如果你運行nox --list --sessions lint,Nox 將只運行 lint 會話:
nox > Running session lint
nox > Creating virtualenv using python3 in .nox/lint
nox > ...
nox > Session lint was successful.
還有更多選擇和運行會話的方法!你可以在命令行用法中閱讀更多有關調用 Nox 的信息。
針對不同的多個 Python 進行測試
許多項目需要支持一個特定的 Python 版本或者多個 Python 版本。你可以通過給 @nox.session 指定 Python,來使 Nox 針對多個解釋器運行會話。這里有一些例子:
(1)如果你希望會話僅針對 Python 的單個版本運行:
@nox.session(python="3.7")
def test(session):
...
(2)如果你希望會話在 Python 的多個版本上運行:
@nox.session(python=["2.7", "3.5", "3.7"])
def test(session):
...
你會注意到,運行nox --list將顯示此會話已擴展為三個不同的會話:
Sessions defined in noxfile.py:
* test-2.7
* test-3.5
* test-3.7
你可以使用nox --sessions test運行所有 test 會話,也可以使用列表中顯示的全名來運行單個 test 會話,例如,nox --sessions test-3.5。有關選擇會話的更多詳細信息,請參見命令行用法文檔。
你可以在會話的virtualenv配置里,閱讀到更多關於配置會話所用的虛擬環境的信息。
與 conda 一起測試
一些項目,特別是在數據科學社區,需要在 conda 環境中測試其使用的情況。如果你希望會話在 conda 環境中運行:
@nox.session(venv_backend="conda")
def test(session):
...
使用 conda 安裝軟件包:
session.conda_install("pytest")
可以用 pip 安裝軟件包進 conda 環境中,但是最好的實踐是僅使用--no-deps 選項安裝。這樣可以避免 pip 安裝的包與 conda 安裝的包不兼容,防止 pip 破壞 conda 環境。
session.install("contexter", "--no-deps")
session.install("-e", ".", "--no-deps")
參數化
就像 Nox 可以控制運行多個解釋器一樣,它也可以使用nox.parametrize()裝飾器,來處理帶有一系列不同參數的會話。
這是一個簡短示例,使用參數化對兩個不同版本的 Django 進行測試:
@nox.session
@nox.parametrize("django", ["1.9", "2.0"])
def test(session, django):
session.install(f"django=={django}")
session.run("pytest")
如果運行nox --list ,你將會看到 Nox 把一個會話擴展為了多個會話。每個會話將獲得你想傳遞給它的一個參數值:
Sessions defined in noxfile.py:
* test(django='1.9')
* test(django='2.0')
nox.parametrize() 的接口和用法特意類似於pytest的parametrize。這是 Nox 的一項極其強大的功能。你可以在參數化會話上,閱讀更多有關參數化的信息與示例。
(譯注:關於 pytest 和其它主流測試框架是如何使用參數化功能的?請參閱《Python 中如何實現參數化測試?》)
下一步
看看你!你現在基本上是一個 Nox 專家啦!✨
到了這一步,你還可以:
玩得開心!💜
相關鏈接:
[1] nox tutorial: https://nox.thea.codes/en/stable/tutorial.html
[2] pip: https://pip.readthedocs.org/
[4] pipx: https://packaging.python.org/guides/installing-stand-alone-command-line-tools/
[5] docker: https://www.docker.com/
[6] thekevjames/nox鏡像: https://hub.docker.com/r/thekevjames/nox
[7] GitHub Actions中: https://github.com/features/actions
[8] Activatedleigh/setup-nox action: https://github.com/marketplace/actions/setup-nox
[9] flake8: http://flake8.pycqa.org/en/latest/
[10] 命令行用法: https://nox.thea.codes/en/stable/usage.html
[11] 配置&API: https://nox.thea.codes/en/stable/config.html
[12] nox.sessions.Session.run(): https://nox.thea.codes/en/stable/config.html%23nox.sessions.Session.run#nox.sessions.Session.run
[13] 會話的virtualenv配置: https://nox.thea.codes/en/stable/config.html%23virtualenv-config#virtualenv-config
[14] nox.parametrize(): https://nox.thea.codes/en/stable/config.html%23nox.parametrize#nox.parametrize
[15] pytest的parametrize: https://pytest.org/latest/parametrize.html%23_pytest.python.Metafunc.parametrize#_pytest.python.Metafunc.parametrize
[16] 參數化會話上: https://nox.thea.codes/en/stable/config.html%23parametrized#parametrized
[17] 貢獻: https://nox.thea.codes/en/stable/CONTRIBUTING.html

公眾號【Python貓】, 本號連載優質的系列文章,有喵星哲學貓系列、Python進階系列、好書推薦系列、技術寫作、優質英文推薦與翻譯等等,歡迎關注哦。
