user1172468
user1172468

Reputation: 5474

Why is my py_test passing tests when it should be failing them?

Here is the code that demonstrates what I'm seeing:

class Test:
    def foo(self):
        return 1

def test_one():
   return Test().foo() == 1

def test_two():
   return Test().foo() == 2

When I run pytest on this code I expect the first test to pass and the second to fail.

Yet what I see is this:

collected 2 items                                                                                                          

test.py ..                                                                                                           [100%]

==================================================== 2 passed in 0.01s =====================================================

I suspect I'm doing something really stupid but I cant seem to figure it out.

Upvotes: 0

Views: 481

Answers (1)

Aviv Yaniv
Aviv Yaniv

Reputation: 6298

First, there is nothing stupid, everyone working with PyTest once has been a beginner!

Change return to assert:

class Test:
    def foo(self):
        return 1

def test_one():
   assert Test().foo() == 1

def test_two():
   assert Test().foo() == 2

Output:

============================= test session starts =============================
platform win32 -- Python 3.8.2, pytest-5.3.0, py-1.8.1, pluggy-0.13.1
rootdir: C:\Users\AvivYaniv\Workspace\SandBox
collected 2 items

test.py .F

================================== FAILURES ===================================
C:\Users\AvivYaniv\Workspace\SandBox\test.py:9: assert 1 == 2
========================= 1 failed, 1 passed in 0.07s =========================

Upvotes: 3

Related Questions