Home > Back-end >  pytest is not running any tests
pytest is not running any tests

Time:10-22

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

CodePudding user response:

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 =====

CodePudding user response:

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

  • Related