python - python测试套件执行完成后执行函数

标签 python unit-testing python-unittest

我正在使用 python unittest 框架进行一些测试。

class AbstractTest(unittest.TestCase):
  def setUp(self):


  def tearDown(self):
    # Close!
    self.transport.close()

  def testVoid(self):
    self.client.testVoid()

  def testString(self):
    global test_basetypes_fails
    try:
      self.assertEqual(self.client.testString('Python' * 20), 'Python' * 20)
    except AssertionError, e:
      test_basetypes_fails = True
      print test_basetypes_fails
      raise AssertionError( e.args )
    try:
      self.assertEqual(self.client.testString(''), '')
    except AssertionError, e:
      test_basetypes_fails = True
      raise AssertionError( e.args )

  def testByte(self):
    global test_basetypes_fails
    try:
      self.assertEqual(self.client.testByte(63), 63)
    except AssertionError, e:
      test_basetypes_fails = True
      raise AssertionError( e.args )
    try:
      self.assertEqual(self.client.testByte(-127), -127)
    except AssertionError, e:
      test_basetypes_fails = True
      raise AssertionError( e.args )

  @classmethod
  def tearDownClass(cls):
    #sys.exit(1)

当我执行测试时,我得到以下结果。

..................
----------------------------------------------------------------------
Ran 18 tests in 2.715s

OK

我需要在执行完成后执行一段程序。我怎样才能做到这一点?当我将代码添加到类级别拆卸时,它会在执行以下部分输出后执行它。

..................

最佳答案

您需要编写自己的测试运行程序,以便可以根据套件的结果返回退出代码。

您需要做的所有事情都在单元测试模块文档中进行了解释。使用 TestLoader 加载您的套件,并使用 TextTestRunner 运行它。然后根据套件的结果,使用适当的退出代码调用 sys.exit。

关于python - python测试套件执行完成后执行函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25402293/

相关文章:

python - 断言 numpy.array 相等性的最佳方法?

python - 有没有一种方法可以将我自己的 CLI 开关传递给单元测试文件?

python - 你如何运行 Openerp 7 内置的单元测试?

python - 运行 'python setup.py test' 而不运行 build_ext?

unit-testing - 单元测试由使用 angular2-jwt AuthHttp 调用而不是 http 调用的服务注入(inject)的组件

python - 如何让 py.test 测试接受交互式输入?

python - 解析Json文件并保存特定值

python - 从 Tensorflow 中的模型检查点将损失值加载到 numpy.array

python - python中的e.printStackTrace等价物

angular - typescript + jasmine.createSpy()