0

I have a unit test that tests if an api point is unaccessible if not authenticated like this:

def test_endpoint_get_unauth(self):
    r = self.get('/api/endpoint/1')
    self.assertStatusCode(r, 401)

The test passes, but nosetests/unittest still shows me an error that an exception was raised saying "not authorized." Is there anyway to stop this?

Full log:

ERROR in views [/myenv/lib/python2.7/site-packages/flask_restless/views.py:115]:
Not Authorized
--------------------------------------------------------------------------------
Traceback (most recent call last):
  File "/myapp/myenv/lib/python2.7/site-packages/flask_restless/views.py", line 113, in decorator
    return func(*args, **kw)
  File "/myapp/myenv/lib/python2.7/site-packages/flask/views.py", line 84, in view
    return self.dispatch_request(*args, **kwargs)
  File "/myapp/myenv/lib/python2.7/site-packages/flask/views.py", line 149, in dispatch_request
    return meth(*args, **kwargs)
  File "/myapp/myenv/lib/python2.7/site-packages/flask_restless/views.py", line 989, in get
    preprocessor(instance_id=instid)
  File "/myapp/app/api/api.py", line 16, in check_auth
    raise ProcessingException(message='Not Authorized', status_code=401)
ProcessingException
................
----------------------------------------------------------------------
Ran 16 tests in 15.788s

OK
Patrick Yan
  • 2,338
  • 5
  • 25
  • 33
  • 2
    Your information is contradictory - your test passes, but fails. You do not provide enough information, is here the code, which raises the exception? – Jan Vlcinsky May 16 '14 at 18:17
  • @JanVlcinsky I believe OP is saying that this test expects endpont to throw not authorized status code, so when endpoint under test fails, the test should pass. The problem is, while endpoint correctly throws not authorized error, test treats it not as expected situation, but as error (which differs from test failure). – J0HN May 16 '14 at 18:21
  • @PatricYan Could you show us the output from testing? Is it about this test case, or about another one? – Jan Vlcinsky May 16 '14 at 18:24
  • @JanVlcinsky, J0HN above is right. The test **passes** (and does not fail), because the status code is 401 (what the `assertStatusCode` is testing). However, in the Terminal, I still get a log of the *error* that the endpoint was accessed while not authorized. – Patrick Yan May 16 '14 at 18:26
  • @PatrickYan You talk about Terminal. What process is printing into it? Nosetests? Or Flask? Or both? If Flask is also throwing the log lines there, then it is natural, it would report unauthorized access as that is exactly what your test case does. – Jan Vlcinsky May 16 '14 at 18:29
  • @JanVlcinsky - Added the full log as proof. The error is obviously from this test case (see the last line). The error also does not occur when this specific test case is removed. – Patrick Yan May 16 '14 at 18:30

3 Answers3

2

The message you see comes likely from application logging.

"ERROR" is the logging level of the event, you need to set the logger threshold to something higher.

Assuming your testcase object has an app attribute with the test client app, if you don't want to see the error message for that test, you write:

def test_endpoint_get_unauth(self):
    import logging
    self.app.logger.setlevel(logging.CRITICAL)

    r = self.get('/api/endpoint/1')
    self.assertStatusCode(r, 401)

For details see https://docs.python.org/2/library/logging.html

RockyRoad
  • 640
  • 5
  • 17
0

You can use the nose option --logging-level=ERROR to set the logging level per run as well.

Josh J
  • 6,813
  • 3
  • 25
  • 47
-1

The error does not come from failed test case, but from tested application, which is complaining about unauthorized access.

This is natural, as your test case is just trying to make the unauthorized access.

As both, tested application and test suite are using stdout for output, it confuses you. You could play a bit with configuring logging of the app and test suite to get it to different files and you would see, who is telling what.

Jan Vlcinsky
  • 42,725
  • 12
  • 101
  • 98
  • Sorry, but this doesn't answer my question. My question was *how* I can suppress the output of errors for successful tests. I already know that my test case is not failing, and I haven't been able to find out how to do what I want in nosetests. – Patrick Yan May 16 '14 at 18:34
  • @PatrickYan I added an advice to configure logging from your application and from test unite to different streams/files. This shall be resolving your question. I guess, the easiest would be to configure Flask app to log into file only. – Jan Vlcinsky May 16 '14 at 18:44