Skip to main content
 首页 » 编程设计

python之Pytest 与线划定的 json

2024年11月24日9sharpest

我是 Python 的新手,也是 pytest 的新手。无论如何,我正在尝试编写一些测试来解析以行分隔的 json 格式的推文。这是一个简化的示例 test_cases.jsonl:

{"contributors":null,"coordinates":null,"created_at":"Sat Aug 20 01:00:12 +0000 2016","entities":{"hashtags":[{"indices":[97,116],"text":"StandWithLouisiana"}]}} 
{"contributors":null,"coordinates":null,"created_at":"Sat Aug 20 01:01:35 +0000 2016","entities":{"hashtags":[]}} 

我想做的是测试如下函数:

def hashtags(t): 
    return ' '.join([h['text'] for h in t['entities']['hashtags']]) 

我可以按如下方式测试单行 JSON:

@pytest.fixture 
def tweet(file='test_cases.jsonl'): 
    with open(file, encoding='utf-8') as lines: 
        for line in lines: 
            return json.loads(line) 
 
 
def test_hashtag(tweet): 
    assert hashtags(tweet) == 'StandWithLouisiana' 

(我只是将文件名作为此示例函数的参数)

这在测试通过的意义上是有效的,因为第一行通过了测试,但我基本上想做的是类似这样的事情,我不希望它像它写的那样工作。

def test_hashtag(tweet): 
    assert hashtags(tweet) == 'StandWithLouisiana' # first tweet 
    assert hashtags(tweet) == ''    # second tweet 

这会失败,因为它测试第一条推文(json 中的行)是否为空,而不是第二条推文。我假设这是因为 fixture 中的 return,但是如果我尝试 yield 而不是 return,我会得到一个 yield_fixture 函数有多个 'yield' error`(第二行仍然失败)。

为了解决这个问题,我现在正在做的是让每一行成为一个单独的 JSON 文件,然后为每一行创建一个单独的 fixture。 (对于较短的示例,我正在使用 StringIO 编写内联 JSON)。 这确实有效,但感觉不雅。我觉得我应该为此使用 @pytest.mark.parametrize,但我无法理解它。我想我也尝试过 pytest_generate_tests 来执行此操作,但它会测试每个键。是否可以按照我的想法进行操作,或者当我对断言有不同的值时创建单独的装置是否更好?

请您参考如下方法:

我认为最适合您的方法是对 fixture 进行参数化:

import json 
import pathlib 
import pytest 
 
 
lines = pathlib.Path('data.json').read_text().split('\n') 
 
@pytest.fixture(params=lines) 
def tweet(request): 
    line = request.param 
    return json.loads(line) 
 
 
def hashtags(t): 
    return ' '.join([h['text'] for h in t['entities']['hashtags']]) 
 
 
def test_hashtag(tweet): 
    assert hashtags(tweet) == 'StandWithLouisiana' 

这将对 tweet 的每个返回值调用一次 test_hashtag:

$ pytest -v 
... 
test_spam.py::test_hashtag[{"contributors":null,"coordinates":null,"created_at":"Sat Aug 20 01:00:12 +0000 2016","entities":{"hashtags":[{"indices":[97,116],"text":"StandWithLouisiana"}]}}] 
test_spam.py::test_hashtag[{"contributors":null,"coordinates":null,"created_at":"Sat Aug 20 01:01:35 +0000 2016","entities":{"hashtags":[]}}] 
... 

编辑:扩展 fixture 以提供预期值

您可以将预期值包含到 tweet fixture 参数中,然后将其原封不动地传递给测试。在下面的示例中,预期的标签与文件行一起压缩以构建 (line, tag) 形式的对。 tweet fixture 将行加载到字典中,传递标签,因此测试中的 tweet 参数变成一对值。

import json 
import pathlib 
import pytest 
 
 
lines = pathlib.Path('data.json').read_text().split('\n') 
expected_tags = ['StandWithLouisiana', ''] 
 
@pytest.fixture(params=zip(lines, expected_tags), 
                ids=tuple(repr(tag) for tag in expected_tags)) 
def tweet(request): 
    line, tag = request.param 
    return (json.loads(line), tag) 
 
 
def hashtags(t): 
    return ' '.join([h['text'] for h in t['entities']['hashtags']]) 
 
 
def test_hashtag(tweet): 
    data, tag = tweet 
    assert hashtags(data) == tag 

测试运行像以前一样产生两个测试:

test_spam.py::test_hashtag['StandWithLouisiana'] PASSED 
test_spam.py::test_hashtag[''] PASSED 

编辑 2:使用间接参数化

另一种可能更简洁的方法是让 tweet fixture 只处理从原始字符串解析推文,将参数化移动到测试本身。我正在使用 indirect parametrization将原始行传递给此处的 tweet fixture:

import json 
import pathlib 
import pytest 
 
 
lines = pathlib.Path('data.json').read_text().split('\n') 
expected_tags = ['StandWithLouisiana', ''] 
 
@pytest.fixture 
def tweet(request): 
    line = request.param 
    return json.loads(line) 
 
 
def hashtags(t): 
    return ' '.join([h['text'] for h in t['entities']['hashtags']]) 
 
 
@pytest.mark.parametrize('tweet, tag',  
                         zip(lines, expected_tags), 
                         ids=tuple(repr(tag) for tag in expected_tags), 
                         indirect=('tweet',)) 
def test_hashtag(tweet, tag): 
    assert hashtags(tweet) == tag 

测试运行现在还会产生两个测试:

test_spam.py::test_hashtag['StandWithLouisiana'] PASSED 
test_spam.py::test_hashtag[''] PASSED