Rafael C.
Rafael C.

Reputation: 2345

Pytest - no tests ran

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?

Upvotes: 29

Views: 44622

Answers (8)

Gio Ordonez
Gio Ordonez

Reputation: 1

I had the same issue as well. What you are missing for pytest to pick up your tests is to ensure that your class has the prefix of Test.

In your case it should look like this:

class TestRegisterNewInstructor:
    def setup_class(cls):
       pass

    def test_01_clickBecomeTopButtom(self):
       pass

    def teardown_class(cls):
       pass

Cheatsheet:

folder -> tests/

file -> prefix test_*

class -> prefix Test*

class methods -> prefix test_*

Prefixing the class with Test is in the docs here: https://docs.pytest.org/en/7.1.x/getting-started.html#group-multiple-tests-in-a-class

Upvotes: 0

alecxe
alecxe

Reputation: 473873

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_ or end with _test in its filename.

Upvotes: 34

I got the same message as shown below

$ pytest -q -rP

no tests ran in 0.10s

Because even though my test file name is my_test_ex1.py in my_tests/ as shown below:

django-project
 |-core
 |  └-settings.py
 |-my_app1
 |-my_app2
 |-my_tests
 |  |-__init__.py
 |  └-my_test_ex1.py # Here
 |-pytest.ini
 ...

I did not set my_test_*.py to python_files in pytest.ini as shown below. *By default, Pytest can run the files test_*.py and *_test.py according to the doc if you do not set python_files in pytest.ini:

# "pytest.ini"

[pytest]
DJANGO_SETTINGS_MODULE = core.settings
# python_files = my_test_*.py # Here

So, I set my_test_*.py to python_files as shown below, then I could run the tests in my_test_ex1.py. *My answer and my answer explain it more:

# "pytest.ini"

[pytest]
DJANGO_SETTINGS_MODULE = core.settings
python_files = my_test_*.py # Here

Upvotes: 0

Serawit
Serawit

Reputation: 21

I found my problem in the pytest.ini file where I had pyton_files = tests_*.py. Removed the 's' and everything worked smoothly.

Upvotes: 2

natielle
natielle

Reputation: 426

If you use conda environment, don't forget to activate it!

Upvotes: 0

Nikolay Kulachenko
Nikolay Kulachenko

Reputation: 4884

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.

Upvotes: 33

Chien Huang
Chien Huang

Reputation: 11

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

Upvotes: 1

Tenzin
Tenzin

Reputation: 2505

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

Upvotes: 0

Related Questions