py.test не * кажется * запускает все тесты - и не терпит неудачу - что происходит? - PullRequest
0 голосов
/ 09 апреля 2020

Контекст

Я следовал учебному пособию и получил код для работы, хотя проблема, которая мне кажется, заключается в том, что в моем классе тестирования есть 6 тестов, и при запуске теста вместо того, чтобы видеть «собрал 6 предметов», я вижу «собрал 5 предметов». Я подозреваю, что тест "test_new_filename" не запущен.

Это предполагаемое поведение pytest или что-то не так с моим кодом?

Исходный код

Вот базовый модуль: assignment4.py

#!/usr/bin/python
import os


class ConfigDict(dict):

    def __init__(self, filename):
        self._filename = filename
        # if path not valid - then raise IOError
        if not os.path.isfile(self._filename):
            try:
                open(self._filename, 'w').close()
            except IOError:
                raise IOError('arg to configdict must be a valid path')
        with open(self._filename) as fp:
            for line in fp:
                line = line.rstrip()
                key, val = line.split('=', 1)
                dict.__setitem__(self, key, val)

    def __setitem__(self, key, value):

        # dict.self[key] = value
        dict.__setitem__(self, key, value)

        if os.path.isfile(self._filename):
            with open(self._filename, 'w') as fw:
                for key, val in self.items():
                    fw.write('{0}={1}\n'.format(key, val))

    def __getitem__(self, key):
        if not key in self:
            raise ConfigKeyError(self, key)
        return dict.__getitem__(self, key)

    @property
    def filename(self):
        return self._filename


class ConfigKeyError(Exception):
    def __init__(self, dictionary, key):
        self.key = key
        self.keys = dictionary.keys()

    def __str__(self):
        # print 'calling str'
        return "The key: {0} does not exist in mydict, avialable keys are {1}".format(self.key, self.keys)

Вот модуль тестирования: test_assignment4.py

from assignment4 import ConfigDict, ConfigKeyError

import os
import pytest
import shutil

class TestConfigDict:
    existing_fn = 'config_file.txt'
    existing_fn_template = 'config_file_template.txt'
    new_fn = 'config_file_new.txt'
    bad_path = '/some/awful/path/that/doesnt/exist/file.txt'

    def setup_class(self):
        shutil.copy(TestConfigDict.existing_fn_template,
                    TestConfigDict.existing_fn)

    def teardown_class(self):
        os.remove(TestConfigDict.new_fn)

    # checking if the object is what we expect
    def test_obj(self):
        cd = ConfigDict(TestConfigDict.existing_fn)
        assert isinstance(cd, ConfigDict)
        assert isinstance(cd, dict)

    # check if the filename gets changed/set in the instance
    # assuming that the file exists
    def test_new_filename(self):
        cd = ConfigDict(TestConfigDict.existing_fn)
        assert cd._filename == TestConfigDict.existing_fn

    # check if using a new filename results in a new file
    # check if using new filename results in the _filename
    #   gets changed in the new instance
    # check if file actually gets created
    def test_new_filename(self):
        assert not os.path.isfile(TestConfigDict.new_fn)
        cd = ConfigDict(TestConfigDict.new_fn)
        assert cd._filename == TestConfigDict.new_fn
        assert os.path.isfile(cd._filename)

    # it should throw an IO error, as the file does not exist
    def test_bad_filepath(self):
        with pytest.raises(IOError):
            ConfigDict(TestConfigDict.bad_path)

    def test_read_dict(self):
        cd = ConfigDict(TestConfigDict.existing_fn)
        assert cd['a'] == '5'
        assert cd['b'] == '10'
        assert cd['c'] == 'this=that'

        with pytest.raises(ConfigKeyError):
            print cd['x']

    def test_write_dict(self):
        cd = ConfigDict(TestConfigDict.existing_fn)
        cd['zz'] = 'top'
        cd2 = ConfigDict(TestConfigDict.existing_fn)
        assert cd2['zz'] == 'top'

и, наконец, вот шаблон файла теста: config_file_template.txt

a=5
b=10
c=this=that

Наблюдаемое поведение

Это вывод, который я вижу при запуске py.test assignment4.py: enter image description here

Что я пробовал

Я попытался закомментировать другие тесты и оставить тест "test_new_filename"; и он показывает «собранный 1 предмет» - что хорошо (я думаю); однако, если я оставлю все тесты без комментариев, я вижу только 5!

1 Ответ

1 голос
/ 09 апреля 2020

У вас есть два теста с одинаковым именем:

# check if the filename gets changed/set in the instance
# assuming that the file exists
def test_new_filename(self):
    cd = ConfigDict(TestConfigDict.existing_fn)
    assert cd._filename == TestConfigDict.existing_fn

# check if using a new filename results in a new file
# check if using new filename results in the _filename
#   gets changed in the new instance
# check if file actually gets created
def test_new_filename(self):
    assert not os.path.isfile(TestConfigDict.new_fn)
    cd = ConfigDict(TestConfigDict.new_fn)
    assert cd._filename == TestConfigDict.new_fn
    assert os.path.isfile(cd._filename)

Второе определение переопределяет первое по стандартным Python правилам именования. Поэтому первый тест можно найти только в том случае, если второй не существует. Это можно исправить, просто изменив названия теста (я рекомендую сделать их более конкретными; переместить часть этого контекста из комментариев в название теста).

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...