Good afternoon guys. I can't do my python test at all ((in the main.py file I added code:

def main(req): print('!__REQ:', req) return "ff" 

In the file test.py added:

 import unittest from main import main class TestMethods(unittest.TestCase): REQUEST = { "GET_POSTS": """GET /api/v1/posts HTTP/1.1""", "POST_POSTS": """POST /api/v1/posts HTTP/1.1 title=TestTitle1""", "GET_404": """GET /api/v1/gimme404 HTTP/1.1""", } RESPONSE = { "EMPTY_200": """HTTP/1.1 200 OK []""", "404": "HTTP/1.1 404 Not Found", "GET_POSTS": """HTTP/1.1 200 OK [{"id": "1", "title": "TestTitle1"}]""" } def test_empty_get(self): self.assertEqual(self.RESPONSE['EMPTY_200'], main(self.REQUEST['GET_POSTS'])) def test_404_get(self): self.assertEqual(self.RESPONSE['404'], main(self.REQUEST['GET_404'])) def test_post_and_get(self): main(self.REQUEST['POST_POSTS']) self.assertEqual(self.RESPONSE['GET_POSTS'], main(self.REQUEST['GET_POSTS'])) if __name__ == '__main__': unittest.main() 

and when I try to run it, it gives me errors like:

FAIL: test_404_get ( main .TestMethods)

 Traceback (most recent call last): File "C:/Users/ROGER/Desktop/python-test-project/test.py", line 29, in test_404_get self.assertEqual(self.RESPONSE['404'], main(self.REQUEST['GET_404'])) AssertionError: 'HTTP/1.1 404 Not Found' != 'ff' - HTTP/1.1 404 Not Found + ff 

What does this mean?

  • The trace also says that the test failed because: 'HTTP / 1.1 404 Not Found'! = 'Ff' - different string values - Dmitry Erohin
  • I can translate this too, but where to dig? what he doesn't like I don't understand - Roger
  • You are in the test comparing the equivalence of two lines. Strings are not equivalent => test drops with AssertionError. They are not equivalent due to the fact that your method returns 'ff', and the test expects 'HTTP / 1.1 404 Not Found'. He doesn’t like it exactly; - Alexey Reytsman
  • I removed the "ff" code and left a clean return "" and still the same error. - Roger

1 answer 1

In the test, you are trying to compare the HTTP / 1.1 404 Not Found string with the value returned by the main function. At first you returned ff, and accordingly the test dropped, since "HTTP / 1.1 404 Not Found"! = "Ff". Later you began to return an empty string, and the test continued to fall for the same reason. You compare exactly what the function returns . And the call to the print command simply works with the console and has nothing to do with the return . Read in detail about what a return is, and you will immediately understand everything!