如何知道每个单元测试用例声明语句所花费的时间

时间:2017-11-25 11:09:43

标签: python unit-testing benchmarking

我知道SO中已经有question了。但我的问题与此不同。

我使用unit-test对脚本进行负载测试。我想知道每个测试用例花费的时间,它只在assert函数中运行。在运行断言之前,我的单元测试正在做一些工作,比如生成测试数据,加载数据库表和其他东西。我不想为这些活动计时。要记住,每个测试用例的数据加载并不常见,所以我不能把它放在setUp函数中。

现在让我们进入代码 -

def setUp(self):
    #set up the env

def tearDown(self):
    #tear down everything

def test_fast_case(self):
    #filing up the directories with test data
    self.assertEqual(myscript(param), 1, "Fast!") # I only want the time taken to execute this step

def test_slow_case(self):
    #filing up the directories with test data
    #calling some other function
    self.assertEqual(myscript(param), 100, "Slow!") # I only want the time taken to execute this step

是的!我知道有一个简单的解决方案,只是为了在assert语句之前和之后保留一个计时器然后打印它。但是我必须在每个测试函数中复制粘贴3行代码。我正在寻找一个通用而强大的解决方案。

0 个答案:

没有答案