KeyError: 'TEST' during running pytest

Hi,

Really need some help for running pytest here.
Environment:
MacOS, Python 3.7 with Anaconda

I didn’t change the SECRET_KEY during init setup of the environment, and at the start the pytest was able to run (during Unit 1 for pytest -m connection). Then recently I realize it should be changed. But after change the .ini SECRET_KEY value, I’ve been running into this key error, and change it back to original value don’t solve the problem. Basically all the test met this error.

def __getitem__(self, key):
    if key != self.default_section and not self.has_section(key):
      raise KeyError(key)

E KeyError: ‘TEST’

Really appreciate if someone can provide some suggestion.
Thanks in advance.

Hi @XUZHENG_03440,

Please check the indentation of .ini file. If you cannot solve it, please share the contents of the .ini file here.

Kanika

Hi Kanika:

Thank you very much for the suggestion. I try to replace the .ini with original one from handout, even before change the mongo url, it has met the same error.

My .ini is as below (url is changed, “#” is double quoted to avoid it show as headline format).

(mflix) linxz-MAC:m220py linxz$ vi .ini

“# Ticket: Connection”
“# Rename this file to .ini after filling in your MFLIX_DB_URI and your SECRET_KEY”
“# Do not surround the URI with quotes”

[PROD]
SECRET_KEY = super_secret_key_you_should_change
MFLIX_DB_URI = mongodb+srv://“my atlas instance url”
MFLIX_NS = sample_mflix

[TEST]
SECRET_KEY = super_secret_testing_key
MFLIX_DB_URI = mongodb+srv://“my atlas instance url”
MFLIX_NS = sample_mflix

Try after removing double quotes.

Kanika

Hi Kanika:

It’s just for replacement. In the file there is no double quotes. It doesn’t work.

mongodb+srv://m220student:m220password@mflix-xxxx.gcp.mongodb.net

And for more information. After python run.py, I can get the correct key from website. So just the pytest is not working in my case.

Thanks.

Hi @XUZHENG_03440,

Which version of pytest you are using?

Kanika

Hi Kanika:

It is v3.3.0.

(mflix) linxz-MAC:m220py linxz$ pytest --version

This is pytest version 3.3.0, imported from /anaconda3/envs/mflix/lib/python3.7/site-packages/pytest.py

setuptools registered plugins:

pytest-flask-0.10.0 at /anaconda3/envs/mflix/lib/python3.7/site-packages/pytest_flask/plugin.py

Hi @XUZHENG_03440,

Please share the messages and lets debug it. :slight_smile:

Kanika

Hi Kanika,

Thanks. The whole error message for ‘pytest -m connection’ is as below:

(mflix) linxz-MAC:tests linxz$ pytest -m connection

====================================== test session starts ======================================
platform darwin – Python 3.7.3, pytest-3.3.0, py-1.8.0, pluggy-0.6.0
rootdir: /Users/linxz/university/m220py/tests, inifile:
plugins: flask-0.10.0
collected 43 items

test_db_connection.py EEEE [100%]

============================================ ERRORS =============================================
______________________________ ERROR at setup of test_atlas_setup _______________________________

request = <SubRequest ‘_configure_application’ for <Function ‘test_atlas_setup’>>
monkeypatch = <_pytest.monkeypatch.MonkeyPatch object at 0x10ee87ac8>

@pytest.fixture(autouse=True)
def _configure_application(request, monkeypatch):
    """Use `pytest.mark.options` decorator to pass options to your application
    factory::

        @pytest.mark.options(debug=False)
        def test_something(app):
            assert not app.debug, 'the application works not in debug mode!'

    """
    if 'app' not in request.fixturenames:
        return
  app = request.getfuncargvalue('app')

/anaconda3/envs/mflix/lib/python3.7/site-packages/pytest_flask/plugin.py:109:


conftest.py:14: in app
app.config[‘SECRET_KEY’] = config[‘TEST’][‘SECRET_KEY’]


self = <configparser.ConfigParser object at 0x10e91b278>, key = ‘TEST’

def __getitem__(self, key):
    if key != self.default_section and not self.has_section(key):
      raise KeyError(key)

E KeyError: ‘TEST’

/anaconda3/envs/mflix/lib/python3.7/configparser.py:958: KeyError
______________________________ ERROR at setup of test_basic_movies ______________________________

request = <SubRequest ‘_configure_application’ for <Function ‘test_basic_movies’>>
monkeypatch = <_pytest.monkeypatch.MonkeyPatch object at 0x10f0c5470>

@pytest.fixture(autouse=True)
def _configure_application(request, monkeypatch):
    """Use `pytest.mark.options` decorator to pass options to your application
    factory::

        @pytest.mark.options(debug=False)
        def test_something(app):
            assert not app.debug, 'the application works not in debug mode!'

    """
    if 'app' not in request.fixturenames:
        return
  app = request.getfuncargvalue('app')

/anaconda3/envs/mflix/lib/python3.7/site-packages/pytest_flask/plugin.py:109:


conftest.py:14: in app
app.config[‘SECRET_KEY’] = config[‘TEST’][‘SECRET_KEY’]


self = <configparser.ConfigParser object at 0x10e91b278>, key = ‘TEST’

def __getitem__(self, key):
    if key != self.default_section and not self.has_section(key):
      raise KeyError(key)

E KeyError: ‘TEST’

/anaconda3/envs/mflix/lib/python3.7/configparser.py:958: KeyError
___________________________ ERROR at setup of test_search_by_movie_id ___________________________

request = <SubRequest ‘_configure_application’ for <Function ‘test_search_by_movie_id’>>
monkeypatch = <_pytest.monkeypatch.MonkeyPatch object at 0x10ef7bd30>

@pytest.fixture(autouse=True)
def _configure_application(request, monkeypatch):
    """Use `pytest.mark.options` decorator to pass options to your application
    factory::

        @pytest.mark.options(debug=False)
        def test_something(app):
            assert not app.debug, 'the application works not in debug mode!'

    """
    if 'app' not in request.fixturenames:
        return
  app = request.getfuncargvalue('app')

/anaconda3/envs/mflix/lib/python3.7/site-packages/pytest_flask/plugin.py:109:


conftest.py:14: in app
app.config[‘SECRET_KEY’] = config[‘TEST’][‘SECRET_KEY’]


self = <configparser.ConfigParser object at 0x10e91b278>, key = ‘TEST’

def __getitem__(self, key):
    if key != self.default_section and not self.has_section(key):
      raise KeyError(key)

E KeyError: ‘TEST’

/anaconda3/envs/mflix/lib/python3.7/configparser.py:958: KeyError
___________________________ ERROR at setup of test_simple_text_search ___________________________

request = <SubRequest ‘_configure_application’ for <Function ‘test_simple_text_search’>>
monkeypatch = <_pytest.monkeypatch.MonkeyPatch object at 0x10f0633c8>

@pytest.fixture(autouse=True)
def _configure_application(request, monkeypatch):
    """Use `pytest.mark.options` decorator to pass options to your application
    factory::

        @pytest.mark.options(debug=False)
        def test_something(app):
            assert not app.debug, 'the application works not in debug mode!'

    """
    if 'app' not in request.fixturenames:
        return
  app = request.getfuncargvalue('app')

/anaconda3/envs/mflix/lib/python3.7/site-packages/pytest_flask/plugin.py:109:


conftest.py:14: in app
app.config[‘SECRET_KEY’] = config[‘TEST’][‘SECRET_KEY’]


self = <configparser.ConfigParser object at 0x10e91b278>, key = ‘TEST’

def __getitem__(self, key):
    if key != self.default_section and not self.has_section(key):
      raise KeyError(key)

E KeyError: ‘TEST’

/anaconda3/envs/mflix/lib/python3.7/configparser.py:958: KeyError
====================================== 39 tests deselected ======================================
============================ 39 deselected, 4 error in 0.23 seconds =============================

Hi @XUZHENG_03440,

From which directory are you running the tests?

It seems like you are running the tests from mflix-python/tests directory. So, it is not able to read .ini file at all.
Please move back one directory and run the tests from mflix-python directory.

Kanika

2 Likes

Hi Kanika:

Thanks a lot for pointing that out. I didn’t realize pytest need to read the .ini file.

Yes my ‘pytest -m connection’ runs ok on the mflix-python directory.

Thank you very much.

1 Like