You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

pytest无法收集测试用例:执行返回no tests found/Empty suite

Python API自动化测试pytest无法收集测试用例问题求助

问题现象

通过PyCharm运行测试或终端执行python -m pytest、pytest命令时,均返回无测试用例被收集:

PyCharm运行输出:

/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

终端执行输出:

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 ==========================================================================================

项目结构

Project structure

相关代码

test_http_methods_here.py

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

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

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()

已尝试的解决方案

  • 在各目录添加/移除__init__.py文件
  • 在各目录添加/移除conftest.py文件
  • 修改workfiles文件夹名称为test/tests
  • 将所有方法/函数命名为test_...格式
  • 设置pytest为默认测试运行器
  • 将autom_api_project目录设为项目结构中的Content Root
  • 清理缓存并重启PyCharm
  • 在根目录添加空的tests.py文件
  • 其他已遗忘的尝试

现寻求该问题的有效解决方案。

内容的提问来源于stack exchange,提问作者bull1t

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 02:45:18