2015-07-21 27 views
4

我使用Python的行爲來做我的測試。在步驟文件中,我想獲取當前步驟名稱,因爲如果測試失敗,我將截屏並將文件重命名爲步驟名稱。如何使用Python獲取當前的行爲步驟?

事情是這樣的:

鑑於用戶登錄
使用者做
然後有事

而且我希望我一步的代碼是這樣的:

@given('user is logged in') 
try: 
    # MY CODE HERE 
except: 
    # GET THE STEP NAME HERE 

任何人都有一個想法如何做到這一點?

回答

5

我已經完成了基本上你正在嘗試做的事情,通過在我的environment.py文件中設置after_step掛鉤來拍攝失敗測試的截圖。

def after_step(context, step): 
    if step.status == "failed": 
     take_the_shot(context.scenario.name + " " + step.name) 

您很可能希望在那裏了,如果一個步驟可在多個方案中出現的場景名稱。

如果上述方法不爲你工作,不幸的行爲不符合當前步驟就像它爲scenario與當前場景或feature當前功能設置上context一個step場。您可以使用before_step掛鉤執行此類分配,然後在步驟本身中,您可以訪問步驟的名稱context.step.name。喜歡的東西:

def before_step(context, step): 
    context.step = step 

而且步實施:

def step_impl(context): 
    if some_condition: 
     take_the_shot(context.scenario.name + " " + context.step.name) 
+0

** ** take_the_shot是包含方案名稱爲參數的方法? –

相關問題