Как я могу создать экземпляр переменной для каждого из моих PyTests?
В частности, я хочу каждый раз создавать новый объект StringIO()
.
Мой текущий код такой:
output = StringIO()
def info_to_string(text):
output.write(text)
def write_to_file(some_text):
for row in some_text:
info_to_string(row)
Мне нужно output
настраивать каждый раз, когда появляется новое тестовое устройство.
Скопировать и вставить код для тестирования:
from io import StringIO
import pytest
output = StringIO()
def info_to_string(text):
output.write(text)
def write_to_file(some_text):
for row in some_text:
info_to_string(row)
def test_1():
write_to_file(['hello', 'there', 'what', 'is', 'up'])
print(output)
assert output.getvalue() == "hellotherewhatisup"
def test_2():
write_to_file(['nothing', 'much'])
assert output.getvalue() == "nothingmuch"
#This will error as the output is "hellotherewhatisupnothingmuch" if run after test_1
ИтакМне понадобится новый output = stringIO()
для каждого теста.