2011-02-17 53 views
1

有'代碼',它正在不斷髮展。 有一個測試服務器,這個服務器使用'測試代碼'來測試不同平臺的'代碼'。 我的問題涉及'測試代碼'。尋找能夠處理不同平臺/版本的測試模式/方法

正如我所說的,服務器測試不同的平臺。但爲了實現這一點,測試代碼需要能夠處理這些不同的平臺。跟蹤由於使用這些不同平臺而產生的那些「小差異」變得非常困難。而且它變得更加複雜,因爲平臺可以有不同的版本,而且..我必須互換地測試這些平臺的混合。

現在,我做這樣的事情:

test1() 
    if(platform == 'a' && version == '1.4') 
     assert('bla',bla) 
    else if(platform == 'a' && version == '1.5') 
     assert('ble',bla) 
    else if(version == '1.6') 
     assert('blu',bla) 
    ..... 

現在,想象一下這一點,但100倍比較複雜,您可能會看到什麼我處理的現在。所以我在問,如果有人知道某種模式/方法可以更優雅或更強大地處理此問題,即使它涉及對架構進行編碼以支持它。

謝謝你們。

回答

2

如果將複雜性存儲在多態對象中,則可以聚合差異並丟失if語句。好處是您可以將您的平臺與測試代碼的差異分開,畢竟這隻關心爲不同環境選擇做出的 聲明。

下面是我的意思是用Python實現的一個簡單例子。這個想法是,你爲每個你關心的環境配置調用一次你的test1函數。你應該期望的細節是由多態對象處理的。現在你的測試代碼很簡單 - 只需要映射到正確的對象,然後是斷言。

#!/usr/bin/python 

class platform_a(object): 

    def __init__(self, version): 
     self.version = version 
     self.bla_mapping = { 
          '1.4' : 'bla', 
          '1.5' : 'ble', 
          '1.6' : 'blu' 
          } 

     self.bla = self.bla_mapping[self.version] 

# Dummy stubs to demo the test code 
class platform_b(object): 
    def __init__(self): 
     # Obviously, add all platform B specific details here - this is 
     # just an example stub 
     self.bla = 'blu' 

class platform_c(object): 
    def __init__(self): 
     # Obviously, add all platform C specific details here - this is 
     # just an example stub 
     self.bla = 'boo' 

def get_the_platform(): return 'a' 
def get_the_version(): return '1.4' 
def result_of_running_the_real_code(): return 'bla' 

def test1(platform, version): 

    # Map platform names to our polymorphic platform objects 
    env_mapping = dict(
         a = platform_a, 
         b = platform_b, 
         c = platform_c, 
             ) 

    # Instantiate an object corresponding to the unique environment under test 
    environment = env_mapping[platform](version) 

    # Get the result of running the real code in this environment 
    bla = result_of_running_the_real_code() 

    # Test we got what we expected 
    assert(environment.bla, bla) 


# TEST HARNESS TOP LEVEL STARTS HERE 
# The environment is presumably specified by the test harness 
platform = get_the_platform() 
version = get_the_version() 

# Run the test 
test1(platform, version) 
+0

對於遲到的回覆抱歉,感謝您的建議! – ignus 2011-03-14 16:21:43