首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >PyTest设置或销毁变量

PyTest设置或销毁变量
EN

Stack Overflow用户
提问于 2018-05-31 18:47:00
回答 3查看 884关注 0票数 -1

如何为我的每个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()

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-05-31 22:01:43

为了防止将来有人看到这一点,我的方法是创建一个类,重新初始化每个fixture

class WriteToString:
    def __init__(self):
        self.output = StringIO()

    def info_to_string(self, text):
        self.output.write(text)


@pytest.fixture
def write_to_string():
    return WriteToString()

并将测试更改为:

def test_2(write_to_string):
    write_to_file(['nothing', 'much'])
    assert write_to_string.output.getvalue() == "nothingmuch" 
票数 1
EN

Stack Overflow用户

发布于 2018-05-31 19:01:45

我不确定我是否完全理解了您的问题,但您可以像下面这样做,这将在您每次将其传递到测试函数时创建一个新的StringIO实例。如果你想每次都返回一个不同的字符串,我不认为你正在寻找一个fixture,而仅仅是一个泛型函数调用来帮你完成这项工作。

import pytest
from StringIO import StringIO

@pytest.fixture(scope='function')
def info_to_string():
    output = StringIO()
    output.write('blah')
    return output

def test_something(info_to_string):
    assert isinstance(info_to_string, StringIO)
    assert info_to_string.getvalue() == 'blah'
票数 1
EN

Stack Overflow用户

发布于 2018-05-31 21:20:10

我认为测试文件写入操作最简单的方法是使用tempfile

但是,一旦提到StringIO作为测试策略的一部分,我的建议是将文件写入分为两部分。这为StringIO缓冲区提供了一个干净的入口点。

from pathlib import Path

from io import StringIO
import pytest

# Separate your data export to 2 functions, one operating 
# with a filename, other with a file object. 
def to_file(filename, content):
    path = Path(filename)
    if not path.exists():
        path.touch()
    with path.open('a') as f_obj:    
        write_to_file(f_obj, content)  

# This is code under test. It must not be connected to your
# test code in any way, even if you have some version of "monkey-patching"
# in mind.     
def write_to_file(file_object, content):
    """Dump *content* list of strings to *filename* with *writer*."""
    for x in content:
       file_object.write(str(x))    

# This is test with StringIO buffer
def test_write_to_file_with_buffer():
    buffer = StringIO()
    # note you make several calls to write_to_file in one test, not across tests
    # this helps keep tests isolated/independent
    write_to_file(buffer, ['hello', 'there', 'what', 'is', 'up']) 
    write_to_file(buffer, ['nothing', 'much'])
    assert buffer.getvalue() == 'hellotherewhatisupnothingmuch'
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50622504

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档