Looking for a testing pattern/approach capable of handling different platforms/versions
There is the 'code', it is being developed continuously. There is a testing server, this server uses 'test code' to test the 'code' in different platforms. My question involves the 'test code'.
As I said, the server tests for different platforms. But for that to happen, the test code needs to be able to handle those different platforms. It is getting really hard to keep track of those little 'differences' that arise because of the 开发者_StackOverflowusage of those different platforms. And it gets more complicated, because the platforms can have different versions and.. I have to test mixing of those interchangeably.
Right now, I am doing something like:
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)
.....
Now, imagine this but 100x more complicated, and you may see what I am dealing with right now. So I am asking if someone out there knows a pattern/approach to handle this more elegantly or robustly, even if it involves coding an architecture to support it.
Thanks guys.
You can aggregate the differences and lose your if
statements if you store the complexity within polymorphic objects. The benefit is that you can decouple your platform
differences from your testing code, which after all is only concerned with making
assertions for different choices of environment.
Here's a simple example of what I mean, implemented in Python. The idea is that you call your test1
function once for every environment configuration you care about. The details of what you should expect are handled by the polymorphic objects. Now your test code is simple - just a mapping to the correct object, followed by the assertion.
#!/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)
精彩评论