如何以优雅的方式对所有pytest测试执行一系列语句?

2024-04-25 04:06:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在写我的第一个py.test测试,我已经写了如下的测试

class Test_X_Values(object):
   def test_value_1(self, fix_A):
       A = fix_A['first']
       B = fix_A['second']
       # some statements here
       #the following part is common for all
       if ( A.X == True and B.Y == True):
           #perform something. for example
           print 'performing'

   def test_value_2(self, fix_A):
       #some statements
       if ( A.X == True and B.Y == True):
           #perform something. for example
           print 'performing'

所以我决定写一个函数

def check(self, A, B):
   if ( A.X == True and B.Y == True):
      #perform something. for example
      print 'performing'

除了在每个测试结束时写入check(A,B)之外,还有什么优雅的方法在每个测试结束时执行这个函数“check”吗


Tags: andtestselftrueforifvalueexample