@reader074

Как передать tsv файл в аргумент функции pytest?

Задача:
есть регулярка в файле match.txt и есть тест-кейсы в файле tsv.
Формат tsv
query	match
jordan belfort	False
"Иван Петров Petr Volikin

1
moscow Markus"	False
RUSSIA Ivan JOHN	False

Нужно чекнуть регулярка матчит(если true) или не матчит(если false).
Вот мой код
import pytest
import re
import pandas as pd
 
path = 'match.txt'
regexs = []
with open(path, "r", encoding="utf8") as r_file:
	for line in r_file:
		stripped = line.rstrip('\r\n')
		if stripped:
			regexs.append("(" + stripped + ")")
if len(regexs) > 0:
	regex = "|".join(regexs)
	regex = re.compile(regex)

tests = pd.read_csv('tsur.tsv', sep='\t')
@pytest.mark.parametrize(('test_input', 'expected'), ([tests]))

def test_match(test_input, expected):
	assert bool(regex.search(test_input)) is expected

Я понимаю, что ошибка в строке
@pytest.mark.parametrize(('test_input', 'expected'), (tests))

Ошибка
in "parametrize" the number of names (2):
  ('test_input', 'expected')
must be equal to the number of values (2897)


как мне запихнуть это все в цикл?
  • Вопрос задан
  • 67 просмотров
Пригласить эксперта
Ответы на вопрос 1
shabelski89
@shabelski89
engineer
Не понятно что внутри файла с регепсом и зачем он в файле, но вот вам пример как передавать файлы из папки аргументом. Каждый файл .tsv пройдёт отдельным тестом, то есть сколько файлов столько выполнен тест.
import os
import re
import pytest


def get_tsc_files(filepath: str):
    return [
        open(os.path.join(filepath, file)).read().strip('\n')
        for file in os.listdir(filepath)
        if os.path.isfile(os.path.join(filepath, file) and file.endswith('.tsv'))
    ]

def get_regex_emails():
    return re.compile(r'([A-Za-z0-9]+[.-_])*[A-Za-z0-9]+@[A-Za-z0-9-]+(\.[A-Z|a-z]{2,})+')


path = 'C:\Работа'
@pytest.mark.parametrize('regex, checked_file', [(get_regex_emails(), file) for file in get_tsc_files(path)])
def test_match(regex, checked_file):
    assert bool(re.search(regex, checked_file)) is True
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы