Handling Errors - Ticket - Unit Test Errors

Question: The unit test (pytest -m error_handling) seems to fail while the validation code on the web page works.
Here are the errors :slight_smile:

(mflix) F:\mongouni\M220P\tests>pytest -m error_handling
============================= test session starts =============================
platform win32 – Python 3.7.3, pytest-3.3.0, py-1.8.0, pluggy-0.6.0
rootdir: F:\mongouni\M220P, inifile: pytest.ini
plugins: remotedata-0.3.1, openfiles-0.3.2, flask-0.11.0, doctestplus-0.3.0, arr
aydiff-0.3
collected 43 items

test_error_handling.py E [100%]

=================================== ERRORS ====================================
_______________________ ERROR at setup of test_no_error _______________________

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

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

on
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 = getfixturevalue(request, 'app')

c:\anaconda3\lib\site-packages\pytest_flask\plugin.py:110:


c:\anaconda3\lib\site-packages\pytest_flask\pytest_compat.py:3: in getfixtureval
ue
return request.getfixturevalue(value)
conftest.py:14: in app
app.config[‘SECRET_KEY’] = config[‘TEST’][‘SECRET_KEY’]


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

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

E KeyError: ‘TEST’

c:\anaconda3\lib\configparser.py:958: KeyError
============================= 42 tests deselected =============================
=================== 42 deselected, 1 error in 0.44 seconds ====================


The import statements provided me a big hint on which exceptions I should handle for the mflix application.

I am not posting the “detailed answer solution here”
What am I missing, please?

P.S I successfully completed the M220P course and loved it!

It looks like the [TEST] section of the configuration is not filled correctly.

Here is my .ini file ? Could you point out what you think is wrong please ? (P.S I used the same secret key in both sections)

Thanks a lot!

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 = CENSORED
#MFLIX_DB_URI = mongodb+srv://m220student:m220password@mflix-k2qhh.mongodb.net/test?retryWrites=true&w=majority
MFLIX_DB_URI = mongodb+srv://mflixAppUser:mflixAppPwd@mflix-k2qhh.mongodb.net/test?retryWrites=true&w=majority
MFLIX_NS = sample_mflix

[TEST]
SECRET_KEY = CENSORED
#MFLIX_DB_URI = mongodb+srv://m220student:m220password@mflix-k2qhh.mongodb.net/test?retryWrites=true&w=majority
MFLIX_DB_URI = mongodb+srv://mflixAppUser:mflixAppPwd@mflix-k2qhh.mongodb.net/test?retryWrites=true&w=majority
MFLIX_NS = sample_mflix

I don’t see anything obviously wrong. However by your prompt (mflix) F:\mongouni\M220P\tests> I suspect that you are not calling pytest from the appropriate directory. I would go back up one directory with cd before running pytest.

1 Like

Hi Steve, you are 100% right.
May this should be highlighted in the notes or I missed that instruction!
Is there any reason for tests to be run in the folder one level above?
Here is the output
Many thanks.
Anil.

(mflix) F:\mongouni\M220P>pytest -m error_handling
============================= test session starts =============================
platform win32 – Python 3.7.3, pytest-3.3.0, py-1.8.0, pluggy-0.6.0
rootdir: F:\mongouni\M220P, inifile: pytest.ini
plugins: remotedata-0.3.1, openfiles-0.3.2, flask-0.11.0, doctestplus-0.3.0, arr
aydiff-0.3
collected 43 items

tests\test_error_handling.py . [100%]

============================= 42 tests deselected =============================
=================== 1 passed, 42 deselected in 0.39 seconds ===================

(mflix) F:\mongouni\M220P>

It might be because of the configuration file .ini which is in the directory M220P and not in M220P/tests directory. So, it won’t be able to read/parse the config file.

Kanika