Pytest не собирает тесты командой pytest - PullRequest
0 голосов
/ 01 февраля 2019

В моем Project/tests/learn_pytest.py есть базовый тест.Это все в файле:

import pytest
import os

class Learning(tmpdir):
    def create_file(self):
        p = tmpdir.mkdir("sub").join("hello.txt")
        p.write("content")
        print tmpdir.listdir()
        assert p.read() == "content"
        assert len(tmpdir.listdir()) == 1
        assert 0

В командной строке, независимо от того, нахожусь ли я в Project или попадаю на тесты, когда я запускаю pytest, выводятся «собранные 0 элементов».Если я нахожусь в каталоге тестов и выполняю pytest -q learn_pytest.py, происходит то же самое.Что мне здесь не хватает?

Ответы [ 2 ]

0 голосов
/ 01 февраля 2019

Вот оно:

файл test_learn_pytest.py:


def test_create_file(tmpdir):  # tmpdir is a fixture and must be a function parameter
    assert 0, "now that will fail ; change the body to what you want"


0 голосов
/ 01 февраля 2019

ваш модуль (learn_pytest.py) и метод (create_file) должны соответствовать соглашению об именах по умолчанию для pytest (а именно: тестовые файлы и функции должны иметь test_ префикс)

Итак, переименуйте файл в *Например, 1004 * и метод test_create_file

greg@canon:~/tmp/54486852$ echo "def foo(): assert 0" > foo.py
greg@canon:~/tmp/54486852$ pytest -v
=============================================================================================== test session starts ===============================================================================================
platform linux -- Python 3.6.7, pytest-4.0.0, py-1.7.0, pluggy-0.8.0 -- /usr/bin/python3.6
cachedir: .pytest_cache
rootdir: /home/greg/tmp/54486852, inifile:
plugins: devpi-server-4.7.1
collected 0 items                                                                                                                                                                                                 

========================================================================================== no tests ran in 0.00 seconds ===========================================================================================
greg@canon:~/tmp/54486852$ echo "def test_foo(): assert 0" > test_foo.py

greg@canon:~/tmp/54486852$ pytest -v --collect-only
=============================================================================================== test session starts ===============================================================================================
platform linux -- Python 3.6.7, pytest-4.0.0, py-1.7.0, pluggy-0.8.0 -- /usr/bin/python3.6
cachedir: .pytest_cache
rootdir: /home/greg/tmp/54486852, inifile:
plugins: devpi-server-4.7.1
collected 1 item                                                                                                                                                                                                  
<Module 'test_foo.py'>
  <Function 'test_foo'>

=============================================================================================
greg@canon:~/tmp/54486852$ 
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...