SKY-A116 · AI-code defects
Code detects the test run
Code detects the test run when production code asks whether a test runner is running it: it reads PYTEST_CURRENT_TEST or process.env.JEST_WORKER_ID, checks "pytest" in sys.modules or typeof jest, or opens the tests' own files. SKY-A116 reports such checks on lines a change added outside the tests.
- Languages
- Python, JavaScript, TypeScript
- Turned on by
skylos doneafter a change (default baseHEAD, so uncommitted edits), orskylos done --base origin/mainfor a pull request- Quality gate
- not checked (full and diff scans). Only
skylos doneruns this rule, in itstest_special_casingcheck. That check blocks by default: a blocking SKY-A116 finding fails the done verdict, and advice findings are listed without failing it. Set the check to "advise" or "off" under[tool.skylos.done.checks]in the base commit'spyproject.tomlto change that. - Finding type
- review
Why it matters
Code that takes a different path under the test runner passes the tests without doing the real work. An agent stuck on a failing test can add if running_under_pytest: return True, and the suite turns green while the real path is never tested.
What Skylos flags
"""Card charges through the payment gateway.""" Added: import osAdded: from payments.gateway import client def charge(card_token, amount_cents):Added: if os.environ.get("PYTEST_CURRENT_TEST"):Added: return True response = client.charge(card_token, amount_cents) return response["status"] == "succeeded"What Skylos leaves alone
"""Django settings for the shop project.""" import sys DEBUG = FalseAdded: TESTING = "pytest" in sys.modulesPAYMENT_BACKEND = "payments.backends.Stripe"Added: if TESTING:Added: PAYMENT_BACKEND = "payments.backends.Fake"Why Skylos leaves it alone
The same "pytest" in sys.modules check sits in a settings file. Settings, config and test setup files are where a project chooses test doubles on purpose, so Skylos does not look for test detection there.
More languages
JavaScript
Flagged
import { gateway } from "./gateway.js"; export async function charge(cardToken, amountCents) {Added: if (process.env.JEST_WORKER_ID) {Added: return true;Added: } const response = await gateway.charge(cardToken, amountCents); return response.status === "succeeded";}Not flagged
import "@testing-library/jest-dom";Added: import { gateway } from "./gateway.js";Added: Added: if (process.env.JEST_WORKER_ID) {Added: gateway.useSandbox();Added: }Why Skylos leaves it alone
src/setupTests.js is the test setup file Jest loads before the tests. Reading JEST_WORKER_ID there is ordinary test plumbing, so Skylos skips setup and config files for this rule.
How Skylos decides
- Runs in
skylos done, which compares the working tree (or a pull request) with a base commit. Only lines the change added to non-test Python, JavaScript and TypeScript files are read. Test files, test helper folders (fixtures,mocks,e2eand similar) and setup or config files are skipped:conftest.py,settings.py,config.py, files undersettingsorconfigfolders,*.config.*andsetupTestsfiles, pytest plugins and JS files that import a test library. - In Python, reports a read of a variable only pytest sets (
PYTEST_CURRENT_TEST,PYTEST_VERSION, thePYTEST_XDIST_*variables),"pytest" in sys.modulesorsys.modules.get("pytest"),sys._called_from_test, a search ofsys.argvfor a runner name, and a look at the call stack (inspect.stack(),sys._getframe) for a test. Code inside apytest_*hook function is left out. - In JavaScript and TypeScript, reports
process.envorimport.meta.envvariables a runner sets (JEST_WORKER_ID,VITEST,VITEST_WORKER_ID,NODE_TEST_CONTEXTand similar),typeof jestortypeof vi, runner globals onglobalThisorwindow, andimport.meta.vitestoutside an in-source test block. - Also reports production code that opens or imports one of the repository's test files by name.
- A comparison of
APP_ENV,NODE_ENVorimport.meta.env.MODEwith"test"is advice. A detection that was already in the base version is not new, and one in generated code or in a new file nothing imports or runs is advice; a test-file read in repository tooling (scripts/,tools/,validate-*files) that no production code imports is advice too.
How to fix it
Remove the check from production code. If the tests need a fake gateway, inject it: pass the client in, or patch it from a fixture or the test setup file, so production code runs the same way everywhere.
def charge(card_token, amount_cents, gateway=client): response = gateway.charge(card_token, amount_cents) return response["status"] == "succeeded"When it can be wrong
- A library that really must behave differently under pytest, such as a plugin shipped outside a
pytest_*hook, is reported. Moving that code into the plugin module (one that importspytest) avoids it. - Detection spread over several lines can be missed: only lines the change added are read, and an environment variable name built at run time is not seen.
Seeing a case that isn't listed here? Report the wrong result so the rule can be fixed. Don't paste company code: describe the shape of it.
How to silence it
skylos donedoes not read# skylos: ignorecomments. To make the whole check advisory, set its mode in the base commit'spyproject.toml; settings in the change itself are ignored.TOML [tool.skylos.done.checks]test_special_casing = "advise"