Get name of current nox session

341 views Asked by At

How do I get the name of the currently running nox session? For example, to use the name to customize a coverage file:

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/')
2

There are 2 answers

0
drhagen On

This was recently implemented as Session.name and will be included in the next release.

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}'
    session.run('python', '-m', 'pytest', '--cov', 'tests/')

Full disclosure: I wrote the PR that added this.

0
drhagen On

This information is available at 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/')

Because this requires accessing a private attribute, it should be considered unstable in addition to being undocumented.