【问题标题】:How to get a single outcome from each python unit-test module如何从每个 python 单元测试模块中获取单个结果
【发布时间】:2019-12-17 01:26:59
【问题描述】:

如您所见,我有不同的 unit_test 模块,它们包含在单元测试代码中:

    import unittest

    class TestTest(unittest.TestCase):
        def setUp(self):
            pass

        def tearDown(self):
            pass

        def test_type(self):
            self.assertTrue(False)

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

我需要得到每个模块运行后的结果并写入数据库。所以我预计在运行每个模块后会有 3 种不同的结果:

  • 通过
  • 未通过
  • 工作中

但问题是在运行测试模块后我得到了这样的输出。

F ==================================================== ===================== 失败:test_test (ma​​in.TestTest) -------------------------------------------------- -------------------- 回溯(最近一次通话最后): 文件“/Users/~/mytest.py”,第 12 行,在 test_test self.assertTrue(False) AssertionError: False 不正确

----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (failures=1)

我不知道 unittest 中是否有一些东西可以像我期望的那样给我一个直接的值?

我也试过这个:

    TestTest = unittest.TestLoader().loadTestsFromTestCase(TestTest)
    suit = unittest.TestSuite([TestTest])
    result = unittest.TextTestRunner(verbosity=2).run(suit)

    print(result)

但它给了我这个:

test_test (ma​​in.TestTest) ... 失败

======================================================================
FAIL: test_test (__main__.TestTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/~/mytest.py", line 12, in test_test
    self.assertTrue(False)
AssertionError: False is not true

----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (failures=1)

【问题讨论】:

标签: python unit-testing


【解决方案1】:

基本上,如果您在测试结果中需要更多信息,只需添加关键字msg 记录测试,例如:

self.assertTrue(False, msg = 'Host connection not stablished.')

【讨论】:

  • 我不需要更多信息,我只需要编译后的每个模块的状态。像 OK、Failed、WORKING 之类的东西
  • 当你运行它时,它会自动进入“ok”或“Failure”状态。不确定“工作”
  • 你为什么不在测试里面打印出来?
猜你喜欢
  • 2015-01-08
  • 2013-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-08
  • 2010-11-23
相关资源
最近更新 更多