Pytest - no tests ran

蹲街弑〆低调 提交于 2021-01-26 19:23:59

问题


I'm using pytest and selenium. When I try run my test script:

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

The message shown is: no tests ran in 0.84 seconds

Could someone help me run this simple test?


回答1:


According to the pytest test conventions, your class should start with Test to be automatically picked up by the test discovery mechanism. Call it TestRegisterNewInstructor instead.

Or, subclass the unittest.TestCase:

import unittest

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

Also keep in mind that the .py test script itself must begin with test_ in its filename.




回答2:


As simple as it looks:

  1. Make sure that your file name matches the pattern: test_*.py or *_test.py.
  2. Make sure that your function name starts with the test prefix.

Find more about the pytest conventions here.




回答3:


try add @classmethod on the top of setUP and tearDown.




回答4:


Did you execute the class itself?
I don't see in this code you are showing that you call the class or definitions to run.
For example, in python you run a class or definition like this:

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


来源:https://stackoverflow.com/questions/34363388/pytest-no-tests-ran

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!