2

I have some pytest tests that connect to a test Postgres DB. These work well locally. However, on GitHub actions, there's no Postgres DB, so I have to disable the entire module that has these tests.

According to the docs, CI Always set to true. So all I have to do is check for the CI env-var, and disable the tests module if it's present and set:

if os.getenv('CI'):
    pytest.skip("No PostgreSQL on GH Actions CI/CD", allow_module_level=True)

Doesn't work.

I have tried setting it manually, setting other env-vars manually via env:, but none of them are visible to Python. Disabling the module locally by negating the above test not os.getenv('CI') works as expected.

What could be the problem?

belteshazzar
  • 2,163
  • 2
  • 21
  • 30

2 Answers2

6

You haven't shown us any any code, but if I set up a repository with the following .github/workflows/environment.yml:

---
name: "Environment test"
on:
  push:
  workflow_dispatch:

jobs:
  show_environment:
    name: "Show environment variables"
    runs-on: ubuntu-latest

    steps:
      - name: "Show environmetn variables"
        run: |
          env

  python_test:
    name: "Reproduce behavior from https://stackoverflow.com/q/65382013/147356"
    runs-on: ubuntu-latest

    steps:
      - name: "Checkout repository"
        uses: actions/checkout@v2

      - name: "Set up python"
        uses: actions/setup-python@v2
        with:
          python-version: 3.8

      - name: "Run python script"
        run: |
          python actiontest.py

And include this actiontest.py in the repository:

import os

if os.getenv('CI'):
    print('Looks like GitHub!')
else:
    print('Maybe running locally?')

It all seems to work as documented. The show environment shows that the CI variable is defined as expected, and the actiontest.py script successfully detects that variable.

If you are seeing different behavior, please include a complete reproducible example in your question and we would be happy to help figure out what's going on.

larsks
  • 277,717
  • 41
  • 399
  • 399
4

Got it. I'm using tox to run the tests among other things. Turns out tox doesn't copy the parent env unless it's told to via the passenv directive. Here's the SO answer that cleared it up.

belteshazzar
  • 2,163
  • 2
  • 21
  • 30