pytest is not running any tests

Question:

My tests are in the following folder structure. Though I have also placed a sample test in the root, which doesn’t work either. I get no tests ran in 6.08s

root/
    test/
        integrations/
                    test_sample.py
                    __init__.py
    __init__.py

I have tried using the command pytest by itself as well as specifying the test location pytest ./test/integrations/test_sample.py

I’ve been using a class to define multiple tests, but simple functions aren’t running either.

Class:

class TestClass:
    def add(self):
        pass

Function:

def add():
    pass
Asked By: Dshiz

||

Answers:

You need to write a test as function with a name beginning with test. See pytest’s docs: test-discovery.

In this test or function:

  1. instantiate your class
  2. and run the class’ method or function
import pytest

# this class will be tested 
class ClassUnderTest:
    def add(self):
        return "it ran"

# This test will run because its name begins with "test"
def test_add():
    cls = ClassUnderTest()
    assert cls.add() == "it ran"

# This won't run because its name does not begin with "test"
def run_add():
    cls = ClassUnderTest()
    assert cls.add() == "it ran"

# ===== 1 passed in 0.05s =====
Answered By: picobit

Methods name should start with test_. (inside class or out) is this what you’re missing?

Answered By: amirhm
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.