Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何通过字典考试_Python_Python 3.x_Pytest - Fatal编程技术网

Python 如何通过字典考试

Python 如何通过字典考试,python,python-3.x,pytest,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 tem

我对编写测试(针对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
导入操作系统
导入系统
进口舒蒂尔
从app.views导入文件\u io
导入pytest
从tempfile导入mkdtemp
导入编解码器
@pytest.fixture()
def tempdir():
tempdir=mkdtemp()
屈服温度
shutil.rmtree(tempdir)
条款=[
[“”、“README.md”、“#Hallo Welt”、“Hallo Welt\n”],
[“test”、“article.md”、“Hallo Welt”、“Hallo Welt\n”]
]
@参数化(“目录,文件,内容,内容,文章)
def test_readRaw(tempdir、dir、file、content_plain、content_md):
dest_path=os.path.join(tempdir,dir)
os.makedirs(dest\u路径,exist\u ok=True)
使用codecs.open(os.path.join(dest_path,file),'w','utf-8')作为fh:
fh.写入(内容普通)
断言文件io.readRaw(os.path.join(dest\u path,file))==content\u 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
文章=[
{“dir”:“,
“文件名”:“README.md”,
“内容”:“你好,世界”,
“content\u html”:“Hello World\n”},
{“dir”:“test”,
“文件名”:“article.md”,
“内容”:“你好”,
“内容”\u html:“你好”}
]
@pytest.mark.parametize(**文章,文章)
def test_readRaw(tempdir,**文章):
使用codecs.open(os.path.join(dir,file),'w','utf-8')作为fh:
fh.write(内容管理)
断言文件\u io.readRaw(os.path.join(dir,file))==content\u md
特别是,我希望避免提及所有键,以便在未修改所有测试的情况下遗漏某些内容时可以扩展字典

也许这是一个愚蠢的问题,但正如我所说,我刚刚开始这个话题,所以我非常感谢每一个提示,我如何才能做到这一点(或者什么是更好的方式)。 顺致敬意,
Dan

不要尝试splat/unplat,而是尝试将
文章
作为参数:

@pytest.mark.parametrize('article',articles)
def test_readRaw(临时目录,文章):
#在这里使用'article['foo']`。。。
另一种选择(利用python3.6+的特性)是手动扩展键——尽管您必须注意以相同的顺序定义每个字典

@pytest.mark.parametrize(tuple(articles[0]),[tuple(dct.values())用于articles中的dct])
def test_readRaw(tempdir、dir、file、content_plain、content_md):
...
无论如何,我认为采用第二种方法会牺牲一些可读性(并使测试特别脆弱)


~相关建议

  • 您可以使用内置的,而不是构建自己的
  • 测试中的函数实际上并不依赖于
    dir
    文件
    ,最好不要将它们参数化
考虑到这两个因素,使用经典参数化(一个简单的输入/输出表)进行测试变得简单得多:

@pytest.mark.parametrize(
('content_plain'、'content_md'),
(
(“哈罗世界”,“哈罗世界”),
(“ohai”、“ohai\n”),
),
)
def测试读取(tmpdir、内容普通、内容md):
f=tmpdir.join('f')
f、 写(内容普通)
断言文件io.readRaw(f)=content\u md

免责声明:我是
pytest

@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