更好用的 Python 任務自動化工具:nox 官方教程


更好用的 Python 任務自動化工具:nox 官方教程

英文| nox tutorial

出處| nox 官方文檔

譯者| 豌豆花下貓@Python貓

Github地址:https://github.com/chinesehuazhou/nox_doc_cn

聲明:本翻譯基於CC BY-NC-SA 4.0授權協議,內容略有改動,轉載請保留原文出處,請勿用於商業或非法用途。

本教程將引導你學會安裝、配置和運行 Nox。

安裝

Nox 可以通過pip輕鬆安裝:

<code>python3-mpipinstallnox
/<code>

你可能希望使用用戶站點(user site)來避免對全局的 Python install 造成混亂:

<code>python3-mpipinstall--usernox
/<code>

或者,你也可以更精緻,使用pipx:

<code>pipxinstallnox
/<code>

無論用哪種方式,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):

<code>importnox

@nox.session
deflint(session):
session.install("flake8")
session.run("flake8","example.py")
/<code>

第一次運行 Nox

現在,你已經安裝了 Nox 並擁有一個配置文件, 那就可以運行 Nox 了!在終端中打開項目的目錄,然後運行nox 。你應該會看到類似這樣的內容:

<code>$nox
nox>Runningsessionlint
nox>Creatingvirtualenvusingpython3.7in.nox/lint
nox>pipinstallflake8
nox>flake8example.py
nox>Sessionlintwassuccessful.
/<code>

✨現在你已第一次成功地使用 Nox 啦!✨

本教程的其餘部分將帶你學習其它可以用 Nox 完成的常見操作。如果需要的話,你還可以跳至命令行用法和配置&API文檔。

安裝依賴項

Nox 基本上是將 session.install 傳遞給 pip ,因此你可以用通常的方式來安裝東西。這裡有一些例子:

(1)一次安裝一個或多個包:

<code>@nox.session
deftests(session):
#sameaspipinstallpytestprotobuf>3.0.0
session.install("pytest","protobuf>3.0.0")
...
/<code>

(2)根據 requirements.txt 文件安裝:

<code>@nox.session 

deftests(session):
#sameaspipinstall-r-requirements.txt
session.install("-r","requirements.txt")
...
/<code>

(3)如果你的項目是一個 Python 包,而你想安裝它:

<code>@nox.session
deftests(session):
#sameaspipinstall.
session.install(".")
...
/<code>

運行命令

session.run 函數可讓你在會話的虛擬環境的上下文中運行命令。以下是一些示例:

(1)你可以安裝和運行 Python 工具:

<code>@nox.session
deftests(session):
session.install("pytest")
session.run("pytest")
/<code>

(2)如果你想給一個程序傳遞更多的參數,只需給 run 添加更多參數即可:

<code>@nox.session
deftests(session):
session.install("pytest")
session.run("pytest","-v","tests")
/<code>

(3)你還可以傳遞環境變量:

<code>@nox.session
deftests(session):

session.install("black")
session.run(
"pytest",
env={
"FLASK_DEBUG":"1"
}
)
/<code>

有關運行程序的更多選項和示例,請參見nox.sessions.Session.run()。

選擇要運行的會話

一旦你的 Noxfile 中有多個會話,你會注意到 Nox 將默認運行所有的會話。儘管這很有用,但是通常一次只需要運行一兩個。

你可以使用--sessions參數(或-s)來選擇要運行的會話。你可以使用--list參數顯示哪些會話可用,哪些將會運行。這裡有一些例子:

這是一個具有三個會話的 Noxfile:

<code>importnox

@nox.session
deftest(session):
...

@nox.session
deflint(session):
...

@nox.session
defdocs(session):
...
/<code>

如果你只運行nox --list ,則會看到所有會話都被選中:

<code>Sessionsdefinedinnoxfile.py:

*test
*lint
*docs

sessionsmarkedwith*areselected,
sessionsmarkedwith-areskipped.
/<code>

如果你運行nox --list --sessions lint,Nox 將只運行 lint 會話:

<code>nox>Runningsessionlint
nox>Creatingvirtualenvusingpython3in.nox/lint
nox>...
nox>Sessionlintwassuccessful.
/<code>

還有更多選擇和運行會話的方法!你可以在命令行用法中閱讀更多有關調用 Nox 的信息。

針對不同的多個 Python 進行測試

許多項目需要支持一個特定的 Python 版本或者多個 Python 版本。你可以通過給 @nox.session 指定 Python,來使 Nox 針對多個解釋器運行會話。這裡有一些例子:

(1)如果你希望會話僅針對 Python 的單個版本運行:

<code>@nox.session(python="3.7")
deftest(session):
...
/<code>

(2)如果你希望會話在 Python 的多個版本上運行:

<code>@nox.session(python=["2.7","3.5","3.7"]) 

deftest(session):
...
/<code>

你會注意到,運行nox --list將顯示此會話已擴展為三個不同的會話:

<code>Sessionsdefinedinnoxfile.py:

*test-2.7
*test-3.5
*test-3.7
/<code>

你可以使用nox --sessions test運行所有 test 會話,也可以使用列表中顯示的全名來運行單個 test 會話,例如,nox --sessions test-3.5。有關選擇會話的更多詳細信息,請參見命令行用法文檔。

你可以在會話的virtualenv配置裡,閱讀到更多關於配置會話所用的虛擬環境的信息。

與 conda 一起測試

一些項目,特別是在數據科學社區,需要在 conda 環境中測試其使用的情況。如果你希望會話在 conda 環境中運行:

<code>@nox.session(venv_backend="conda")
deftest(session):
...
/<code>

使用 conda 安裝軟件包:

<code>session.conda_install("pytest")
/<code>

可以用 pip 安裝軟件包進 conda 環境中,但是最好的實踐是僅使用--no-deps 選項安裝。這樣可以避免 pip 安裝的包與 conda 安裝的包不兼容,防止 pip 破壞 conda 環境。

<code>session.install("contexter","--no-deps")
session.install("-e",".","--no-deps")
/<code>

參數化

就像 Nox 可以控制運行多個解釋器一樣,它也可以使用nox.parametrize()裝飾器,來處理帶有一系列不同參數的會話。

這是一個簡短示例,使用參數化對兩個不同版本的 Django 進行測試:

<code>@nox.session
@nox.parametrize("django",["1.9","2.0"])
deftest(session,django):
session.install(f"django=={django}")
session.run("pytest")
/<code>

如果運行nox --list ,你將會看到 Nox 把一個會話擴展為了多個會話。每個會話將獲得你想傳遞給它的一個參數值:

<code>Sessionsdefinedinnoxfile.py:

*test(django='1.9')
*test(django='2.0')
/<code>

nox.parametrize() 的接口和用法特意類似於pytest的parametrize。這是 Nox 的一項極其強大的功能。你可以在參數化會話上,閱讀更多有關參數化的信息與示例。

(譯註:關於 pytest 和其它主流測試框架是如何使用參數化功能的?請參閱《 》《 》)

下一步

看看你!你現在基本上是一個 Nox 專家啦!✨

到了這一步,你還可以:

  • 閱讀更多文檔,例如命令行用法和配置&API。
  • 給我們反饋或作貢獻,請參閱貢獻。

玩得開心!

相關鏈接:

Github地址:https://github.com/chinesehuazhou/nox_doc_cn

[1] nox tutorial: https://nox.thea.codes/en/stable/tutorial.html

[2] pip: https://pip.readthedocs.org/

[3] 用戶站點: https://packaging.python.org/tutorials/installing-packages/%23installing-to-the-user-site#installing-to-the-user-site

[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進階、Python哲學、編程語言、書籍推薦等內容,另外還有官方 PEP 翻譯與優質外文的翻譯,值得關注一同學習。


分享到:


相關文章: