No tests were found / collected 0 items / empty suite
Question:
Currently learning Python to automate API and faced the first significant issue I found no solution for.
When I try to run my test either with Run or with terminal by python -m pytest
or just pytest/py.test
commands, I get this in the Run tab:
/Users/usr/PycharmProjects/project/bin/python /Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pycharm/_jb_pytest_runner.py --path /Users/usr/PycharmProjects/project/tests/test_google_maps_api.py
Testing started at 20:20 ...
Launching pytest with arguments /Users/usr/PycharmProjects/project/tests/test_google_maps_api.py --no-header --no-summary -q in /Users/usr/PycharmProjects/project/tests
test session starts ==============================
collecting ... collected 0 items
no tests ran in 0.24s =============================
Process finished with exit code 5
Empty suite
Or this in the terminal:
test session starts ===========================================================================================
platform darwin -- Python 3.11.1, pytest-7.2.1, pluggy-1.0.0 rootdir: /Users/usr/PycharmProjects/project
collected 0 items
no tests ran in 0.13s ==========================================================================================
Here is the structure of my project:
And here is the code:
test_http_methods_here.py file:
import requests
class test_http_methods:
headers = {'Content-Type': 'application/json'}
cookies = ""
@staticmethod
def test_get(url):
result = requests.get(url, headers=test_http_methods.headers, cookies=test_http_methods.cookies)
return result
@staticmethod
def test_post(url, body):
result = requests.post(url, json=body, headers=test_http_methods.headers, cookies=test_http_methods.cookies)
return result
@staticmethod
def test_put(url, body):
result = requests.put(url, json=body, headers=test_http_methods.headers, cookies=test_http_methods.cookies)
return result
@staticmethod
def test_delete(url, body):
result = requests.delete(url, json=body, headers=test_http_methods.headers, cookies=test_http_methods.cookies)
return result
test_api.py file:
from workfiles.test_http_methods_here import test_http_methods
base_url = "https://rahulshettyacademy.com"
key = 'key=qaclick123'
class test_google_maps_api():
@staticmethod
def test_create_new_place():
json_for_new_place = {
"location": {
"lat": -38.383494,
"lng": 33.427362
}, "accuracy": 50,
"name": "Frontline house",
"phone_number": "(+91) 983 893 3937",
"address": "29, side layout, cohen 09",
"types": [
"shoe park",
"shop"
],
"website": "http://google.com",
"language": "French-IN"
}
post_resource = '/maps/api/place/add/json'
post_url = base_url + post_resource + key
print (post_url)
result_post = test_http_methods.test_post(post_url, json_for_new_place)
print(result_post.text)
return result_post
test_google_maps_api.py file:
import requests
from requests import Response
from workfiles.test_api import test_google_maps_api
class test_create_new_place():
def test_create_new_place(self):
print("Method POST")
result_post: requests.Response = test_google_maps_api.test_create_new_place()
I have already tried the following:
- Adding and removing
__init__.py
file in every directory
- Adding and removing
conftest.py
file in every directory
- Adding
test/tests
to the workfiles
folder name
- Naming every method/function with
test_...
- Setting up pytest as Default test runner
- Adding my
autom_api_project
directory as Content Root in Project Structure
- Clearing cache and restarting PyCharm after all these
- Adding empty
tests.py
file to the root directory
- Something else I can’t remember
Answers:
Your test classes are not adhering to Pytest’s naming conventions and therefore the discovery fails. Test classes must be named using Test
prefix, that is with uppercase T
.
Currently learning Python to automate API and faced the first significant issue I found no solution for.
When I try to run my test either with Run or with terminal by python -m pytest
or just pytest/py.test
commands, I get this in the Run tab:
/Users/usr/PycharmProjects/project/bin/python /Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pycharm/_jb_pytest_runner.py --path /Users/usr/PycharmProjects/project/tests/test_google_maps_api.py
Testing started at 20:20 ...
Launching pytest with arguments /Users/usr/PycharmProjects/project/tests/test_google_maps_api.py --no-header --no-summary -q in /Users/usr/PycharmProjects/project/tests
test session starts ==============================
collecting ... collected 0 items
no tests ran in 0.24s =============================
Process finished with exit code 5
Empty suite
Or this in the terminal:
test session starts ===========================================================================================
platform darwin -- Python 3.11.1, pytest-7.2.1, pluggy-1.0.0 rootdir: /Users/usr/PycharmProjects/project
collected 0 items
no tests ran in 0.13s ==========================================================================================
Here is the structure of my project:
And here is the code:
test_http_methods_here.py file:
import requests
class test_http_methods:
headers = {'Content-Type': 'application/json'}
cookies = ""
@staticmethod
def test_get(url):
result = requests.get(url, headers=test_http_methods.headers, cookies=test_http_methods.cookies)
return result
@staticmethod
def test_post(url, body):
result = requests.post(url, json=body, headers=test_http_methods.headers, cookies=test_http_methods.cookies)
return result
@staticmethod
def test_put(url, body):
result = requests.put(url, json=body, headers=test_http_methods.headers, cookies=test_http_methods.cookies)
return result
@staticmethod
def test_delete(url, body):
result = requests.delete(url, json=body, headers=test_http_methods.headers, cookies=test_http_methods.cookies)
return result
test_api.py file:
from workfiles.test_http_methods_here import test_http_methods
base_url = "https://rahulshettyacademy.com"
key = 'key=qaclick123'
class test_google_maps_api():
@staticmethod
def test_create_new_place():
json_for_new_place = {
"location": {
"lat": -38.383494,
"lng": 33.427362
}, "accuracy": 50,
"name": "Frontline house",
"phone_number": "(+91) 983 893 3937",
"address": "29, side layout, cohen 09",
"types": [
"shoe park",
"shop"
],
"website": "http://google.com",
"language": "French-IN"
}
post_resource = '/maps/api/place/add/json'
post_url = base_url + post_resource + key
print (post_url)
result_post = test_http_methods.test_post(post_url, json_for_new_place)
print(result_post.text)
return result_post
test_google_maps_api.py file:
import requests
from requests import Response
from workfiles.test_api import test_google_maps_api
class test_create_new_place():
def test_create_new_place(self):
print("Method POST")
result_post: requests.Response = test_google_maps_api.test_create_new_place()
I have already tried the following:
- Adding and removing
__init__.py
file in every directory - Adding and removing
conftest.py
file in every directory - Adding
test/tests
to theworkfiles
folder name - Naming every method/function with
test_...
- Setting up pytest as Default test runner
- Adding my
autom_api_project
directory as Content Root in Project Structure - Clearing cache and restarting PyCharm after all these
- Adding empty
tests.py
file to the root directory - Something else I can’t remember
Your test classes are not adhering to Pytest’s naming conventions and therefore the discovery fails. Test classes must be named using Test
prefix, that is with uppercase T
.