All jobs

Migrate a Python Test Suite From nose to pytest

Yes. An agent ports nose generator tests, with_setup and nose.tools asserts to pytest and proves the same number of cases still run.

Run this on RushOpens the console with this prompt ready to send, in a Project that has your repository.

What the agent does

nose stopped working with Python 3.10, so the agent's first job is to find out what the suite did when it last ran. In a project like yandex/gixy, requirements.dev.txt asks for nose>=1.3.7, tox.ini runs nosetests -v, .travis.yml runs nosetests --with-coverage --cover-package gixy -v, and setup.py sets test_suite='nose.collector'. On Python 3.11 nosetests dies at start with AttributeError: module 'collections' has no attribute 'Callable'. The agent therefore creates a second environment on Python 3.9, where nose still imports, and records the baseline: 296 tests, of which 44 end in error. Every one of the 44 is NameError: name 'BaseFormatter' is not defined at one line of tests/plugins/test_simply.py, a name the file uses and never imports. That is a defect in the suite as it stands, and the agent reports it before porting anything.

Next it sorts the nose features by how they fail under pytest, since each fails differently. Thirteen test functions are generators that yield a check and its arguments: eleven in tests/core/test_regexp.py, one in tests/parser/test_nginx_parser.py and test_from_config in test_simply.py. Together they produce 233 of the 296 cases. pytest 9.2 refuses them at collection with 'yield' keyword is allowed in fixtures, but not in tests. Eleven tests in test_context.py and test_variable.py use @with_setup(setup, tear_down). pytest ignores that decorator, and without the teardown that calls purge_context() three tests in test_context.py fail on state left by the one before. The nose.tools asserts are the easy part: nose2pytest rewrites all but two calls, both assert_not_equals, an alias it does not know.

The generators become pytest.mark.parametrize, and the agent meets a difference in timing here. nose ran a generator's body when the test ran, after the whole module was imported. A parametrize decorator builds its cases at import, and in two of these files the generators name helpers such as check_positive_contain and assert_config that are defined further down, so the first attempt ends in NameError during collection. The parametrized tests move below their helpers. test_from_config also has code after its last yield that checks every plugin has a positive and a false-positive config; that becomes a test of its own. with_setup becomes one autouse fixture per module that asserts the context stack is empty, yields, then purges it.

Then the agent counts. With the missing import added, pytest on Python 3.11 reports 297 passed: the 296 nose cases and the one new plugin-coverage test. With that import taken out again, 44 fail and 253 pass, the same 44 as the nose baseline, which shows the port neither lost cases nor hid the old failure. One more finding goes in the report unchanged: test_directive in test_nginx_parser.py ends its loop with return assert_config, ... where its neighbour says yield, so it has never asserted anything. pytest flags it with PytestReturnNotNoneWarning.

Start it with one API call

POST https://api.phoenix.so/o/{org}/sessions with this body:

{
  "agent": "claude",
  "input": "Migrate this Python project's tests from nose to pytest. Read the test requirements, tox.ini, setup.py or pyproject.toml, the CI configuration and every file under tests. List each nose feature in use and where: nose.tools asserts, generator tests that yield, with_setup, module or package setup functions, nose plugins, test_suite in setup.py. Run nosetests on the newest Python this machine has and report the result. If it cannot start, create a second environment on Python 3.9 with uv, run nosetests -v there, and record the number of tests and the name of every failure or error; this is the baseline. Report any failure in the baseline as a finding before changing anything. Then port: run nose2pytest for the asserts and fix by hand what it leaves, turn each generator test into pytest.mark.parametrize with the same cases, replace with_setup by fixtures with the same setup and teardown, and move any code that ran after a generator's last yield into its own test. Remove nose from the requirements and from setup.py, and switch tox.ini and CI to pytest. Uninstall nose and run pytest. The collected count must equal the baseline plus any test you added, and every baseline failure must either still fail for the same reason or be fixed in a separate commit that says so. Report tests that return a value or assert nothing. Open a pull request with both runs' output.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
  1. Get an API key. In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
  2. Send the request. POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
  3. Read what came back. Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

What comes back

A pull request with two commits. The first is the port: the test modules, tests/asserts.py without its nose.tools import, requirements.dev.txt, tox.ini, .travis.yml and the test_suite line in setup.py. The second is the one-line import that turns the 44 errors into passes, kept apart so it can be reviewed, or rejected, on its own. The description gives the baseline from nose on Python 3.9, the pytest result with and without the second commit, a table of the thirteen generators with the number of cases each produced before and after, and the test_directive finding.

What a reviewer checks is the arithmetic. A migration that reports all green proves little when the old runner could not start; one that reports 296 before, 297 after, and names the extra test can be verified by reading one table. They also look at the fixture that replaced with_setup, because it now applies to every test in its module and not only to the decorated ones. In these two modules every test was decorated, and the description says so.

Run it on every repository still listing nose

Old Python projects carry the same three nose constructs, so this body works across them. Search your organisation for nose in requirement files and nosetests in tox.ini, then send the body to POST /o/{org}/sessions once per repository with that repository in resources and a request_id that is a UUID v5 of its name, so a repeated batch starts no second run.

Each run opens its own pull request with its own baseline. Runs past the key's max_concurrent are created queued and start in order. The baseline needs an older interpreter, which the agent installs with uv inside its own run, so nothing on your machines has to keep Python 3.9 around.

The use case this job follows, with its automation and trigger: Write the missing tests.

Why run it on Rush?

The proof for a test-runner migration is a count from the old runner, and the old runner no longer starts on a current Python. Getting that number means a second interpreter, a second environment and a full run under each, which is a chore on a laptop and routine in a sandbox the run owns. Both runs' output is kept, so the 296 and the 297 in the pull request are quotations.

It also surfaces what the old suite was hiding. The 44 errors and the test that returns instead of yielding were there before the migration. A port done by search and replace would either carry them along unseen or, worse, make them vanish. Running both suites and comparing them case by case is what makes them visible.

Frequently asked questions

Why not keep nose and pin an old Python?

Because the pin keeps moving against you. nose 1.3.7 fails to start on Python 3.10 and later, and the baseline here needed Python 3.9, which no longer receives fixes. Every dependency that drops old Pythons narrows what the suite can test.

Does pytest still run nose-style tests on its own?

No. Current pytest does not honour with_setup or nose's module-level setup functions, and a test function containing yield is a collection error. Plain test_ functions with bare asserts run unchanged, which is why a partly ported suite can look healthier than it is.

How are yield tests converted?

Each generator becomes a function that returns its cases and a test decorated with pytest.mark.parametrize over them. The case values stay the same, so the count stays the same. The decorator evaluates at import time, so it has to sit below any helper the cases refer to.

Will the agent fix failing tests it finds?

Only in a separate commit, and only when the cause is in the test and not in the code under test. Here the cause is a missing import in a test module. A failure that points at the library itself is reported and left failing.

More jobs like this: Write the missing tests

Keep reading