- Pytest collects tests from modules with names starting with
test_ (although you can specify the module name explicitly when calling pytest), classes for Test and functions for test_ , and can also collect all test modules from the tests folder (I don’t remember the exact rules, I need to look at the documentation) . Accordingly, your function should be called test_function . - Remove from the testing function
return . The testing function is not required to return anything (although this is not prohibited), but in this case, the output occurs before assert , so the check is not performed. In the example from your question, func is a test function, it returns a value, test_answer is a testing function, it does not return anything, it just checks the results of a call to the function under test. assert can be several assert in one function, for example, if we check the response status first, then the presence of some text in the response, etc.
Based on this, the test_module.py file:
import requests def test_function(): resp=requests.get('http://q.com') assert resp.status_code == 200 # сначала проверяем статус assert 'welcome' in resp.text # потом проверяем наличие слова welcome в ответе
Run py.test , we get the result:
============================= test session starts ============================= platform win32 -- Python 3.5.3, pytest-3.5.0, py-1.5.3, pluggy-0.6.0 rootdir: ***, inifile: collected 1 item test_module.py . [100%] ========================== 1 passed in 2.77 seconds ===========================
assert resp.status_code == 200will fail; is afterreturn- gil9red