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

pytest调用外部模块方法显示Empty Suite且无打印输出问题求助

解决pytest报"Empty Suite"且无print输出的问题

嘿,我来帮你分析下问题出在哪,以及怎么修复:

问题根源

你遇到的"Empty Suite"是因为pytest根本没找到可执行的测试用例,而print没输出是因为你的代码逻辑要么没被执行,要么被pytest的输出捕获了。具体来说:

  • test_classB.py的写法完全不符合测试框架规范:代码直接写在类的顶层,不是test_开头的方法,而且Test类既没继承unittest.TestCase,也没遵循pytest的测试用例识别规则,pytest找不到要运行的测试。
  • import self as self是完全错误的写法,self只有在类的实例方法内部才有意义,不能通过导入获取。
  • 手动调用Roboform.setUp(self)这种方式不对,测试框架(不管是unittest还是pytest)会自动管理初始化和清理流程,不需要你手动调用这些方法,而且这里的self也不是Roboform类的实例,会导致错误。

修复方案

我给你两种修复方式,你可以根据习惯选择:

方案1:继续使用unittest风格(兼容pytest)

首先修改roboForm.py,把close_browser改成tearDown——因为unittest会自动在每个测试方法执行完后调用tearDown,不需要手动关闭浏览器:

import unittest
from selenium import webdriver

class Roboform(unittest.TestCase):
    def setUp(self):
        # 创建Chrome会话
        self.driver = webdriver.Chrome()
        self.driver.implicitly_wait(10)
        self.driver.maximize_window()
        self.driver.get("https://www.roboform.com/filling-tests")

    def click_Custom_Form(self):
        # 验证页面标题
        title = self.driver.title
        assert title == "RoboForm Form Filling Tests"
        print("We are on the right page")
        # 点击Custom Form链接
        self.driver.find_element_by_xpath("//*[contains(text(),'Custom')]").click()

    def tearDown(self):
        # 自动关闭浏览器
        self.driver.quit()

然后修改test_classB.py,让它继承Roboform类,并写一个test_开头的测试方法:

from roboForm import Roboform

class TestRoboform(Roboform):
    def test_click_custom_form(self):
        self.click_Custom_Form()
        print("Test Passed")

现在运行测试时,执行命令:

pytest test_classB.py -s

加-s是因为pytest默认会捕获print输出,加上这个参数才能看到你的打印内容。

方案2:改用pytest的Fixture风格(更灵活)

如果你不想用unittest的类继承方式,可以改用pytest的fixture来管理浏览器的初始化和清理:

先修改roboForm.py为普通类(不需要继承unittest.TestCase):

from selenium import webdriver

class Roboform:
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.implicitly_wait(10)
        self.driver.maximize_window()
        self.driver.get("https://www.roboform.com/filling-tests")

    def click_Custom_Form(self):
        title = self.driver.title
        assert title == "RoboForm Form Filling Tests"
        print("We are on the right page")
        self.driver.find_element_by_xpath("//*[contains(text(),'Custom')]").click()

    def close_browser(self):
        self.driver.quit()

然后修改test_classB.py,用pytest fixture来管理Roboform实例的生命周期:

import pytest
from roboForm import Roboform

@pytest.fixture
def roboform():
    # 初始化浏览器
    rf = Roboform()
    rf.setUp()
    yield rf
    # 测试完成后清理
    rf.close_browser()

def test_click_custom_form(roboform):
    roboform.click_Custom_Form()
    print("Test Passed")

同样用pytest test_classB.py -s命令运行,就能看到测试执行,print内容也会输出。

关键要点总结

  • pytest识别测试用例的规则:要么是test_开头的函数,要么是继承unittest.TestCase的类里的test_开头方法。
  • 不要手动调用初始化/清理方法,交给测试框架自动处理。
  • 想要看到print输出,运行pytest时加-s参数关闭输出捕获。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.13 07:55:27