将操作系统表示为枚举
我希望在我们的自动化测试框架中添加测试编写者使用代表测试支持的最低操作系统版本的属性来标记其测试方法的能力。例如,测试编写者会标记测试方法支持 Windows Vista SP1 及更高版本,然后框架将知道在 Vista、7 等上运行测试。
我的直觉是使用枚举来表示此表示(例如 OS. VistaSp1、OS.Win7 等),但由于我们还支持多种操作系统类型(Mac、Linux 等),因此枚举不足以让框架了解最小值(例如,它无法轻易知道OS.Win7 高于 OS.VistaSp1,但与 OS.MacOsX 无关)
我的下一步是我可以使用枚举的较低位来表示操作系统类型,使用较高位来表示[相对]版本号这就能解决问题。它不是最干净的,但是使用它的测试编写者根本不需要意识到这一点,并且只需要解析出信息的少量代码,这些代码都可以封装在某个 Utils 类中。
对这个主题有什么想法吗?这是一个干净的解决方案吗?有更好的解决方案吗? 谢谢!
I am looking to add to our automated test framework the ability for test writers to mark their test methods with attributes representing the minimum OS version the test supports. For example, the test writer would mark that a test method supports Windows Vista SP1 and higher, and then the framework will know to run the test on Vista, 7, etc.
My gut was to use an enum for this representation (e.g. OS.VistaSp1, OS.Win7, etc.) but since we also support several OS types (Mac, Linux, etc.) an enum wouldn't be enough information for the framework to understand what the minimum is (e.g. it couldn't easily know that OS.Win7 is higher than OS.VistaSp1, but not related to OS.MacOsX)
My next though was that I could use the lower bits of the enum to represent the OS type and the higher bits to represent the [relative] version number and that would solve the issue. It's not the cleanest, but the test writers using it wouldn't need to be aware of this at all, and there would only small amount of code that would need to parse out the information, which can all be encapsulate in some Utils class.
Any thoughts on the subject? Is this a clean solution? Is there a better solution?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是否无法使用已经存在的Environment.OperatingSystem 类?
对于平台 ID,它包含 MacOSX、Unix、Win32S、Win32NT、Win32Windows、WinCE 和 XBox。
它还包含服务包的字符串和提供版本号的 Version 对象。
我不知道它自动检测这些的效果如何(我猜单声道支持其中的很多),因为我自己从未尝试过。
Is it not possible to use the already existing Environment.OperatingSystem class?
For platform id it contains MacOSX, Unix, Win32S, Win32NT, Win32Windows, WinCE and XBox.
It also contains a string for the service pack and a Version object to provide a version number.
I don't know how well it detects these automatically (i'm guessing mono supports a bunch of these) as i've never tried it myself.
为此,我将(至少)使用两个不同的变量:
这有两个好处:
* 不是真正的 XP 版本号,只是示例
I would use (at least) two distinct variables for this:
This has two benefits:
* not a real XP version number, just example
我会编写一个操作系统枚举并使用 Version 对象作为第二个参数。
I would write a OS enum and use a Version object as second parameter.
当您需要更多功能时,可以以与枚举类似的方式使用结构体。声明一堆
static readonly
字段,它们是结构的实例,可以像一组枚举值一样使用(当然不在 switch 语句中):显然,您需要在这里做更多的事情,就像可能添加更多字段并重载一些运算符(这里必须使用相等运算符),但这在很多方面都像枚举一样起作用,但提供了更多功能。
You can use a struct in a similar manner to an enum when you need a bit more functionality. Declare a bunch of
static readonly
fields which are instances of your struct to be used like a set of enumerated values (except of course not in a switch statement):Obviously you'll need to do more here, like possibly adding more fields and overloading some of the operators (equality operators are a must here), but this will act in many ways like an enum but provide much more power.