Pytest错误-将对象位置与字符串进行比较

时间:2019-03-24 23:54:35

标签: python python-3.x pytest

我正在尝试从要从文件中提取的数据测试我在python中制成的对象,但我不断收到错误self.assertEquals(Create("input.txt"), "1/2") AssertionError: <dsadsa.Create object at 0x0340EAD0> != '1/2'

我如何获取实际数据而不是对象,而不是存储在Pytest中的数据?

这是我在其中提取数据并创建对象的代码:

class Run:
    def __init__(self, first, second):
        self.first = first
        self.second = second

    def __repr__(self):
        return f"{self.first}/{self.second}"

class Create:
    def __init__(self, file):
        self.file_contents = [line.rstrip("\n)") for line in open(file)]
        self.all = []
        self.add()
        self.prnt()

    def add(self):
        for i in self.file_contents:
            first, second = i.split(",")
            self.all.append(Run(first,second))

    def prnt(self):
        for i in self.all:
            print(i)

def main():
    Create("input.txt")

if __name__ == "__main__":
    main()

这是我的Pytest脚本来测试对象:

import unittest
from dsadsa import Create

class Test(unittest.TestCase):

    def test_one(self):
        self.assertEquals(Create("input.txt"), "1/2")

if __name__ == '__main__':
    unittest.main()

这是我的文件数据:     1,2

对此有任何帮助/建议,将不胜感激。

解决方案-

a = Create("input.txt") # Make an istance and then use the self.all attribute to get data to compare
self.assertEqual(str(a.all[0]), "1/2")

0 个答案:

没有答案