寻找能够处理不同平台/版本的测试模式/方法

发布于 2024-10-18 02:36:42 字数 592 浏览 1 评论 0原文

有“代码”,它正在不断开发。 有一个测试服务器,这个服务器使用“测试代码”来测试不同平台上的“代码”。 我的问题涉及“测试代码”。

正如我所说,服务器针对不同平台进行测试。但要做到这一点,测试代码需要能够处理这些不同的平台。跟踪由于使用这些不同平台而产生的微小“差异”变得非常困难。它变得更加复杂,因为平台可以有不同的版本并且......我必须交替测试这些版本的混合。

现在,我正在做类似的事情:

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 倍的情况,你可能会看到我现在正在处理的事情。所以我问是否有人知道一种模式/方法来更优雅或更稳健地处理这个问题,即使它涉及编码一个架构来支持它。

谢谢你们。

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 usage 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白芷 2024-10-25 02:36:42

如果将复杂性存储在多态对象中,则可以聚合差异并丢失 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)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文