繁体   English   中英

py.test 没有*似乎*运行所有测试 - 并且没有失败 - 发生了什么?

[英]py.test doesn't *seem* to run all tests - and doesn't fail - what is going on?

语境

我一直在关注一个教程,并且让代码可以工作,尽管我似乎遇到的问题是 - 在我的测试 class 和运行测试中有 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时看到的 output : 在此处输入图像描述

我试过的

我试图注释掉其他测试,并留下测试“test_new_filename”; 它显示“已收集 1 件”——这很好(我认为); 但是,如果我不评论所有测试,我只会看到 5 个!

您有两个具有相同名称的测试:

# 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 名称破坏规则覆盖第一个定义。 因此,只有在第二个不存在时才能找到第一个测试。 您可以通过更改测试名称来解决此问题(我建议使它们更具体;将一些上下文从注释中移出并进入测试名称)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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