25

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?

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
Rafael C.
  • 2,245
  • 4
  • 29
  • 45

7 Answers7

30

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.

Al Sweigart
  • 11,566
  • 10
  • 64
  • 92
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • 1
    I'm not sure as to why this answer has had upvotes and is accepted. OP's post was about the `pytest` library, not `unittest`. Maybe I'm wrong. Are they both related under the bonnet? @alecxe @AlSweigart @RafaelC. – StressedBoi69420 Oct 05 '21 at 09:41
  • Pytest will also include a class's methods to test if they subclass unittest.TestCase. I believe this is to make it cross-compatible to ease migration from one to the other. – Al Sweigart Oct 12 '21 at 15:19
26

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.

Nikolay Kulachenko
  • 4,604
  • 4
  • 31
  • 37
1

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

0

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()
Tenzin
  • 2,415
  • 2
  • 23
  • 36
0

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

natielle
  • 380
  • 3
  • 14
0

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

0

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 set test_*.py to python_files in pytest.ini as shown below:

# "pytest.ini"

[pytest]
DJANGO_SETTINGS_MODULE = core.settings
python_files = 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:

# "pytest.ini"

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

In addition, test_*.py and *_test.py are searched by default, if you don't set python_files in pytest.ini as shown below:

# "pytest.ini"

[pytest]
DJANGO_SETTINGS_MODULE = core.settings
# python_files = my_test_*.py
# `test_*.py` and `*_test.py` are searched
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129