其他测试框架 使用 python 测试 C# 的 dll

yuweixx · June 21, 2016 · Last by yuweixx replied at June 22, 2016 · 1486 hits

公司的产品主要由 C# 编写,所以前段时间做了下自动化测试 C# 程序的预研,拿出来分享一下

工具选项及缺陷

  • 使用 iconpython 调用 C# 接口;使用 unittest 进行测试工作,包括数据断言、用例收集、用例执行等。
  • 缺陷,iconpython 是一个支持 python2.x 语法的语言,并不等于 python,可以调用 python 的基本库,但不可以调用第三方 python 库。

环境搭建

  1. 安装 VS;
  2. 安装 iconpython;
  3. VS 中创建一个.net 项目;
  4. 在.net 项目中添加 iconpython 文件;
  5. 将被测 dll 移至项目中,并添加引用;

示例代码

#导入需要的python库
import clr
import msvcrt
import unittest
#导入必要的.NET库
clr.AddReferenceByPartialName("System.Windows.Forms")
clr.AddReferenceByPartialName("System.Drawing")
from System.Windows.Forms import *
from System.Drawing import *
#导入被测.NET库
clr.AddReferenceToFile("xxx.dll")
from xx.xx.xx import *
#导入其它python文件这的测试用例
from module1 import testCase3
from module2 import testCase4
#一个测试用例
class testCase1(unittest.TestCase):
    #用例执行前的准备动作
    def setUp(self):
        a=1
    #用例执行完后的清场动作
    def tearDown(self):
        a=0
    #用例主体
    def runTest1(self):
        a = xx.xxx
        b = a.ToString()
        print b
        #断言
        self.assertFalse(a)
#用例集收集
def suite():
    suite = unittest.TestSuite()
    suite.addTest(testCase1("runTest1"))
    suite.addTest(testCase3("runTest3"))
    suite.addTest(testCase4("runTest4"))
    return suite
#用例集执行
if __name__ == '__main__':
    unittest.main(defaultTest = 'suite')

测试结果

参考资料

共收到 4 条回复 时间 点赞

既然装了 vs 为什么还要用 python 测试.....
vs 自带的 test 库也很强大啊

示例代码怎么没有? 请用代码代替截图吧
测试 native 的 c 的 so 也可以用类似的方法去测试. 这样就可以脱离原生语言的各种低端测试框架的不足了

yuweixx #3 · June 22, 2016 Author

#1 楼 @dongdong 只是找方案而已,并非实践,VS 的 test 库当然也行。

yuweixx #4 · June 22, 2016 Author

#2 楼 @seveniruby 已改成代码块

需要 Sign In 后方可回复, 如果你还没有账号请点击这里 Sign Up