class Cls:
def setUp(self):
self.name = 'sunxiaochuan'

def test_print(self):
print(self.name)


if __name__ == '__main__':
c = Cls()
c.test_print() #AttributeError: 'Cls' object has no attribute 'name'
import unittest


class Cls(unittest.TestCase):
def setUp(self):
self.name = 'sunxiaochuan'

def test_print(self):
print(self.name)


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


↙↙↙阅读原文可查看相关链接,并与作者交流