2015-09-01 101 views
1

在PyCharm中,我設置了py.test作爲默認測試運行器。PyCharm + cProfile + py.test - > pstat快照視圖+調用圖爲空

我有一個簡單的測試案例:

import unittest 
import time 

def my_function(): 
    time.sleep(0.42) 

class MyTestCase(unittest.TestCase): 

    def test_something(self): 
     my_function() 

現在我運行通過右鍵單擊該文件,並選擇Profile 'py.test in test_profile.py'測試。

我看到測試在控制檯上成功運行(它說collected 1 items)。但是,顯示生成的pstat文件的Statistics/Call Graph視圖爲空,並且表示Nothing to show

我期望看到test_somethingmy_function的分析信息。我究竟做錯了什麼?

編輯1: 如果我更改文件名來一些東西,確實開始與test_,取出unittest.TestCase並插入一個__main__方法調用my_function,我終於可以運行CPROFILE沒有py.test和我看到結果。

但是,我正在進行大量的測試工作。我想直接描述這些測試,而不是編寫額外的性能分析腳本。有沒有辦法調用py.test測試發現模塊,以便我可以遞歸地檢索項目的所有測試? (unittest發現是不夠的,因爲我們在發生器功能中產生了很多參數化測試,而unittest未識別這些測試)。這樣我至少可以解決這個問題,只有一個額外的腳本。

回答

0

這是一個解決方法。創建一個包含以下內容的額外的Python腳本(相應地調整路徑測試根):

import os 
import pytest 

if __name__ == '__main__': 
    source_dir = os.path.dirname(os.path.abspath(__file__)) 
    test_dir = os.path.abspath(os.path.join(source_dir, "../")) 
    pytest.main(test_dir, "setup.cfg") 

腳本文件名不能以test_啓動,否則pycharm將迫使你與py.test運行它。然後右鍵單擊該文件並使用Profile運行它。

這也適用於與Coverage一起運行它。