如何在 & 之前运行特定代码在 Python 中的每个单元测试之后 [英] How to a run specific code before & after each unit test in Python

查看:29
本文介绍了如何在 & 之前运行特定代码在 Python 中的每个单元测试之后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我在文件中的测试结构.

Following is the structure of my tests in a file.

班级
设置
测试01
测试02
测试03
拆解

Class
 setup
  test01
  test02
  test03
 teardown

我要求在每次测试前后运行特定代码.

I have a requirement to run specific code before and after each test.

之前,我可以从设置中调用该代码.但是在测试之后,我不知道该怎么做.显然,从拆卸中调用代码适用于最后一个测试,但我如何让它在中间的测试中运行?

For before, I could invoke that code from the setup. But for after the test, I am not able to figure how to do it. Obviously invoking the code from teardown would work for the last test, but how can I have it run for the tests in between?

推荐答案

假设您正确地使用了一个继承自 unittest.TestCase 的类,那么 setUp 方法在每次测试之前运行,tearDown 方法在每次测试之后运行.查看文档.所以把你的代码放在这两种方法中是完全可行的.

Assuming that you're properly using a class descended from unittest.TestCase, then the setUp method is run before each test, and the tearDown method is run after each test. Check the documentation. So it's completely feasible to put your code in those two methods.

这篇关于如何在 & 之前运行特定代码在 Python 中的每个单元测试之后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆