首页 > 解决方案 > 获取当前 nox 会话的名称

问题描述

如何获取当前运行的 nox 会话的名称?例如,要使用名称来自定义覆盖文件:

import nox


@nox.session(python=['3.6', '3.7', '3.8', '3.9'])
def test(session):
    session.install('.')
    session.install('pytest', 'pytest-cov')
    session.env['COVERAGE_FILE'] = f'.coverage.{session.name}'  # <-- what do I put here?
    session.run('python', '-m', 'pytest', '--cov', 'tests/')

标签: pythonnox

解决方案


此信息可在以下网址获得Session._runner.friendly_name

import nox


@nox.session(python=['3.6', '3.7', '3.8', '3.9'])
def test(session):
    session.install('.')
    session.install('pytest', 'pytest-cov')
    session.env['COVERAGE_FILE'] = f'.coverage.{session._runner.friendly_name}'
    session.run('python', '-m', 'pytest', '--cov', 'tests/')

因为这需要访问私有属性,所以除了未记录之外,它还应该被认为是不稳定的。


推荐阅读