简体   繁体   中英

Why do I only get a function as a return value by using a fixture (from pytest) in a test script?

I want to write test functions for my code and decided to use pytest. I had a look into this tutorial: https://semaphoreci.com/community/tutorials/testing-python-applications-with-pytest

My real code involves another script, written by me, so I made an example, which also creates the same problem, but does not rely on my other code.

@pytest.fixture()
def example():
    value = 10
    return value

def test_value(example):
    print(example)
    assert(example == 10)

test_value(example)

When I run my script with this toy example, the print returns a function:

<function example at 0x0391E540>

and the assertion fails.

If I try to call example() with the parenthesis, I get this:

Failed: Fixture "example_chunks" called directly. Fixtures are not meant to be called directly, but are created automatically when test functions request them as parameters. See https://docs.pytest.org/en/stable/fixture.html for more information about fixtures, and https://docs.pytest.org/en/stable/deprecations.html#calling-fixtures-directly about how to update your code.

I am sure, I am missing something important here, but searching google did not help me, which is why I hope somebody here can provide some assistance.

  1. Remove this line from your script
test_value(example)
  1. Run your script file with pytest file.py

Fixtures will be automatically resolved by pytest

In your example you run code directly and fixtures are just simple functions

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM