Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KeyError: 'PYTEST_CURRENT_TEST' when running in pytest #2559

Closed
acmiyaguchi opened this issue Nov 15, 2019 · 4 comments
Closed

KeyError: 'PYTEST_CURRENT_TEST' when running in pytest #2559

acmiyaguchi opened this issue Nov 15, 2019 · 4 comments

Comments

@acmiyaguchi
Copy link
Contributor

The latest version of moto (1.3.14) is breaking in the following test scenario:

test.py

import pytest
from moto import mock_s3
import boto3

@pytest.fixture()
def client():
    mock_s3().start()
    client = boto3.resource("s3")
    client.create_bucket(Bucket="test-bucket")
    yield client
    mock_s3().stop()


def test_moto_fixture(client):
    return True

tox.ini

[tox]
envlist = py27
skipsdist = True

[testenv]
deps=
    pytest
    boto3
    moto == 1.3.14
commands=
    python -m pytest test.py {posargs}

Running tox with these two files at the root returns the following error.

self = {}, key = 'PYTEST_CURRENT_TEST', args = ()

    def pop(self, key, *args):
        unsetenv(key)
>       return self.data.pop(key, *args)
E       KeyError: 'PYTEST_CURRENT_TEST'

.tox/py27/lib/python2.7/os.py:505: KeyError

Setting moto == 1.3.13 fixes this.

@acmiyaguchi acmiyaguchi changed the title KeyError: 'PYTEST_CURRENT_TEST' when running test KeyError: 'PYTEST_CURRENT_TEST' when running in pytest Nov 15, 2019
@bblommers
Copy link
Collaborator

Running this with Python37 gives a bit more background information:

>       mock_s3().stop()

test.py:11: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
.tox/py37/lib/python3.7/site-packages/moto/core/models.py:75: in stop
    self.env_variables_mocks.stop()
.tox/py37/lib/python3.7/site-packages/mock/mock.py:1813: in __exit__
    self._unpatch_dict()
.tox/py37/lib/python3.7/site-packages/mock/mock.py:1805: in _unpatch_dict
    in_dict.update(original)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

args = [None], kwds = {}, self = environ({}), other = None

    def update(*args, **kwds):
        ''' D.update([E, ]**F) -> None.  Update D from mapping/iterable E and F.
            If E present and has a .keys() method, does:     for k in E: D[k] = E[k]
            If E present and lacks .keys() method, does:     for (k, v) in E: D[k] = v
            In either case, this is followed by: for k, v in F.items(): D[k] = v
        '''
       ...
E               TypeError: 'NoneType' object is not iterable

.tox/py37/lib/python3.7/_collections_abc.py:846: TypeError

The erring line was introduced in this PR: #2285

@bblommers
Copy link
Collaborator

In your test, you're introducing two instances of the class 'mock_s3'. The PR above introduced class-level variables to mock AWS credentials, so it's now required to call 'start()' before calling 'stop()' on the same class-instance.

Changing your test case to either one of the following setups will work:

import pytest
from moto import mock_s3
import boto3


@pytest.fixture()
@mock_s3()
def client():
    client = boto3.resource("s3")
    client.create_bucket(Bucket="test-bucket")
    yield client


def test_moto_fixture(client):
    return True

Or

import pytest
from moto import mock_s3
import boto3


@pytest.fixture()
def client():
    mock = mock_s3()
    mock.start()
    client = boto3.resource("s3")
    client.create_bucket(Bucket="test-bucket")
    yield client
    mock.stop()


def test_moto_fixture(client):
    return True

@acmiyaguchi
Copy link
Contributor Author

Thanks for the tip, using the same mocking instance fixes the problem that I was seeing. The first setup doesn't propagate the mock_s3 environment into the cases where the fixture is used, though.

@acmiyaguchi
Copy link
Contributor Author

For reference, this is how I generated my testing fixtures: #620 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants