2015-04-15 52 views
2

我有兩個特徵文件:做人,具有相同名稱的多個步驟

delete.feature 
new_directory.feature 

和二步文件:

delete.py 
new_directory.py 

特徵文件中的每一個這樣開始的:

Background: 
    Given 'Workspace has the following structure' 

遵循不同的表格。

當我在步文件裝飾寫:

@given('Workspace has the following structure') 

它是如何知道哪些特徵文件的背景是屬於?當我爲

new_directory.feature

運行的行爲,我可以看到它的運行從delete.feature那一步。除了擁有所有獨特的步驟名稱之外,是否有任何方法可以在這些文件之間做出區別

回答

2

我已經解決了共享步驟的方式,是根據使用該步驟的功能針對不同工作步驟使用單個實現。適用於你的描述,這將是這樣的:

@given('Workspace has the following structure') 
def step_impl(context): 
    feature = context.feature 

    name = os.path.splitext(os.path.basename(feature.filename))[0] 
    if name == "delete": 
     # do something 
     ... 
    elif name == "new_directory": 
     # do something else 
     ... 
    else: 
     raise Exception("can't determine how to run this step") 

上面的代碼是基於檢查基本名稱包含功能的文件(減去擴展名)。您還可以檢查實際功能名稱,但我認爲文件名比功能名稱更穩定,所以我更願意測試文件名。

+0

謝謝,這就是我一直在尋找的! :) –

相關問題