打印测试执行时间并使用py.test确定慢速测试

时间:2015-01-11 05:54:22

标签: python pytest

我正在使用py.test在CI服务器上运行单元测试。测试使用通过网络获取的外部资源。有时测试运行器需要太长时间,导致测试运行器中止。我无法在本地重复这些问题。

有没有办法让py.test打印出(慢)测试的执行时间,这样可以更容易地确定有问题的测试?

2 个答案:

答案 0 :(得分:106)

我不确定这会解决您的问题,但您可以在测试套件完成后通过--durations=N打印最慢的N测试。

答案 1 :(得分:0)

您可以使用class Node: def __init__(self, value): self.value = value self.next = None def __str__(self): return "Node({})".format(self.value) __repr__ = __str__ class Queue: def __init__(self): self.head=None self.tail=None def __str__(self): temp=self.head out=[] while temp: out.append(str(temp.value)) temp=temp.next out=' '.join(out) return ('Head:{}\nTail:{}\nQueue:{}'.format(self.head,self.tail,out)) __repr__=__str__ def isEmpty(self): #write your code here return (self.head == None) and (self.tail == None) def __len__(self): #write your code here current = self.head len = 0 while current: len += 1 current = self.head.next if self.head.next == self.tail: break return len def enqueue(self, value): #write your code here node = Node(value) if self.isEmpty(): self.head = node self.tail = node else: self.tail.next = node self.tail = node def dequeue(self): #write your code here if self.isEmpty(): return 'Queue is empty' else: popped = self.head.value self.head = self.head.next return popped def front(self): if self.head != None: return self.head.value q = Queue() def reversequeue(q): if q.isEmpty() == False: data = q.front() q.dequeue() reversequeue(q) q.enqueue(data) def QueuePalindrome(word): q = Queue() for i in word: q.enqueue(i) p = reversequeue(q) return p

传递数字
--durations

请参考:https://medium.com/@brianokken/pytest-durations-0-show-all-times-for-tests-and-setup-and-teardown-848dccac85db

或者:https://docs.pytest.org/en/latest/usage.html#profiling-test-execution-duration