python - 如何通过字典进行测试

标签 python python-3.x pytest

我在编写测试(python)方面很新,所以我现在有一个问题,如何将字典传递给测试函数?目前我执行以下操作:

import os
import sys
import shutil
from app.views import file_io
import pytest
from tempfile import mkdtemp
import codecs

@pytest.fixture()
def tempdir():
    tempdir = mkdtemp()
    yield tempdir
    shutil.rmtree(tempdir)

articles = [
        ["", "README.md", "# Hallo Welt", "<h1>Hallo Welt</h1>\n"],
        ["test", "article.md", "# Hallo Welt", "<h1>Hallo Welt</h1>\n"]
]


@pytest.mark.parametrize("dir, file, content_plain, content_md", articles)
def test_readRaw(tempdir, dir, file, content_plain, content_md):
    dest_path=os.path.join(tempdir, dir)
    os.makedirs(dest_path, exist_ok=True)

    with codecs.open(os.path.join(dest_path, file), 'w', 'utf-8') as fh:
        fh.write(content_plain)

    assert file_io.readRaw(os.path.join(dest_path, file)) == content_plain

我的想法/希望是我可以修改代码,以便我可以执行以下操作:
articles = [
               { "dir": "",
                 "filename": "README.md",
                 "content_md": "# Hello World",
                 "content_html": "<h1>Hello World</h1>\n" },
               { "dir": "test",
                 "filename": "article.md",
                 "content_md": "# Hallo Welt",
                 "content_html": "<h1>Hallo Welt</h1>\n"}

]


@pytest.mark.parametrize(**articles, articles)
def test_readRaw(tempdir, **articles):
    with codecs.open(os.path.join(dir, file), 'w', 'utf-8') as fh:
        fh.write(content_md)

    assert file_io.readRaw(os.path.join(dir, file)) == content_md

特别是我想避免提及所有键,这样我就可以在没有修改所有测试的情况下遗漏某些内容时扩展字典。

也许这是一个愚蠢的问题,但正如我所说的,我只是从这个话题开始,所以我非常感谢每一个提示我如何做到这一点(或什么是更好的方法)。
此致

最佳答案

与其尝试 splat/unsplat,不如尝试使用 article作为参数:

@pytest.mark.parametrize('article', articles)
def test_readRaw(tempdir, article):
    # use `article['foo']` here...

另一种选择(使用 python3.6+ 特性)是手动扩展键——尽管你必须小心你以相同的顺序定义每个字典

@pytest.mark.parametrize(tuple(articles[0]), [tuple(dct.values()) for dct in articles])
def test_readRaw(tempdir, dir, file, content_plain, content_md):
    ...

对于它的值(value),我认为采用第二种方法会牺牲一些可读性(并使测试特别脆弱)

~相关建议
  • 您可以使用内置 tmp_path / tmpdir fixtures而不是建立自己的
  • 您的被测函数实际上并不依赖于 dirfile ,你最好不要参数化这些

  • 考虑到这两点,使用经典参数化(一个简单的输入/输出表),您的测试变得更加简单:
    @pytest.mark.parametrize(
        ('content_plain', 'content_md'),
        (
            ("# Hallo Welt", "<h1>Hallo Welt</h1>\n"),
            ("# ohai", "<h1>ohai</h1>\n"),
        ),
    )
    def test_readRaw(tmpdir, content_plain, content_md):
        f = tmpdir.join('f')
        f.write(content_plain)
        assert file_io.readRaw(f) == content_md
    

    免责声明:我是 pytest 上的当前核心开发人员之一

    关于python - 如何通过字典进行测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59544165/

    相关文章:

    python - 合并 pandas 数据帧时出现数据类型错误

    Python CLIck,测试线程应用程序

    python - pytest 参数化 fixture 函数和测试函数

    python - 生成器作为函数参数

    mongodb - 在pymodm中按对象查询相关集合?

    python - NameError : name 'hasattr' is not defined - Python3. 6, Django1.11, Ubuntu16-17, Apache2.4, mod_wsgi

    python - << : 'str' and 'int' while reading file 不支持的操作数类型

    python - 解释(和比较)numpy.correlate 的输出

    python - 在另一个列表上应用 lambda 函数列表时的意外行为

    python - Python 新手尝试理解语法