Pytest - 没有测试 运行

Pytest - no tests ran

我正在使用 pytest 和 selenium。当我尝试 运行 我的测试脚本时:

import pytest
from selenium import webdriver
from pages import *
from locators import *
from selenium.webdriver.common.by import By
import time

class RegisterNewInstructor:
    def setup_class(cls):
        cls.driver = webdriver.Firefox()
        cls.driver.get("http://mytest.com")

    def test_01_clickBecomeTopButtom(self):
        page = HomePage(self.driver)
        page.click_become_top_button()
        self.assertTrue(page.check_instructor_form_page_loaded())


    def teardown_class(cls):
        cls.driver.close()

显示的消息是:在 0.84 秒内没有测试 运行

有人可以帮我 运行 这个简单的测试吗?

您是否执行了 class 本身?
我在这段代码中没有看到您显示调用 class 或 运行 的定义。
例如,在 python 中,您 运行 一个 class 或这样的定义:

class Hello():
    # __init__ is a definition runs itself. 
    def __init__(self): 
        print('Hello there')
        # Call another definition. 
        self.andBye()

    # This definition should be calles in order to be executed. 
    def andBye(self):
        print('Goodbye')

# Run class 
Hello()

根据 pytest test conventions,您的 class 应该以 Test 开头,以便测试发现机制自动选取。取而代之的是 TestRegisterNewInstructor

或者,子class unittest.TestCase:

import unittest

class RegisterNewInstructor(unittest.TestCase):
    # ...

另请记住,.py 测试脚本本身的文件名必须以 test_ 开头或以 _test 结尾。

尝试在 setUP 和 tearDown 的顶部添加 @classmethod。

看起来很简单:

  1. 确保您的 文件名 与模式匹配:test_*.py*_test.py.
  2. 确保您的 函数名称test 前缀开头。

查找有关 pytest 约定的更多信息 here