Robot 框架如何使用C库
介绍
这里通过一个简单的例子,演示如何在Robot Framework 的测试库中使用C语言。我们使用Python标准库中的ctypes模块(对于早期Python版本可能未集成,需要另行安装),该模块需要调用C代码编写的共享库。当前的例子我仅在OSX上实现与测试,对于Unix和Linux平台大同小异,对于Windows平台,仅仅需要注意共享库的格式和调用方式即可,当前未做其他平台相关测试。
共享库
第一步,我们需要编写C的共享库。
我们编写的例子是一个非常简单的登录系统(login.c), 通过输入的用户名和密码进行验证,并返回验证结果。这里有两组合法的用户名密码组合:demo/mode和john/long.其他组合都是错误的。下面是login.c的完整代码:
1/* 2Simple system that validates passwords and user names. There are two users in 3system with valid user name and password. "demo mode" and "john long". All 4other user names are invalid. Except that there are bugs. Can you spot them? 5*/ 6 7#include <string.h> 8#define NR_USERS 2 9 10struct User { 11 const char* name; 12 const char* password; 13}; 14const struct User VALID_USERS[NR_USERS] = { "john", "long", "demo", "mode" }; 15 16int validate_user(const char* name, const char* password) { 17 int i; 18 for (i = 0; i < NR_USERS; i++) { 19 if (0 == strncmp(VALID_USERS[i].name, name, strlen(VALID_USERS[i].name))) 20 if (0 == strncmp(VALID_USERS[i].password, password, strlen(VALID_USERS[i].password))) 21 return 1; 22 } 23 return 0; 24}
我们将这个文件编译成共享库liblogin.so. 在当前目录下,我们创建Makefile文件。 Makefile编写如下:
1CC=gcc 2SRC=login.c 3SO=liblogin.so 4 5$(SO): $(SRC) 6 $(CC) -fPIC -shared -o $(SO) $(SRC) 7 8clean: 9 rm -f $(SO)
我们在当前目录执行make命令,就创建了共享库liblogin.so. 后面我们将介绍如何编写Robot Framework测试库来调用我们的C共享库。
测试库
在这里,我们按照Robot框架的规范,来编写测试库LoginLibrary.py. LoginLibrary是一个简单的测试库,通过ctypes模块来与底层的C共享库进行交互。我们这个库仅仅提供了一个关键字就是 Check User.
下面是LoginLibrary.py的完整代码:
1"""Robot Framework test library example that calls C code. 2 3This example uses Python's standard `ctypes` module, which requires 4that the C code is compiled into a shared library. 5 6It is also possible to execute this file from the command line 7to test the C code manually. 8""" 9 10from ctypes import CDLL, c_char_p 11 12LIBRARY = CDLL('./liblogin.so') # On Windows we'd use '.dll' 13 14 15def check_user(username, password): 16 """Validates user name and password using imported shared C library.""" 17 if not LIBRARY.validate_user(c_char_p(username), c_char_p(password)): 18 raise AssertionError('Wrong username/password combination') 19 20 21if __name__ == '__main__': 22 import sys 23 try: 24 check_user(*sys.argv[1:]) 25 except TypeError: 26 print 'Usage: %s username password' % sys.argv[0] 27 except AssertionError, err: 28 print err 29 else: 30 print 'Valid password'
if __name__ == '__main__' 语句块不是用于测试库的,我们在这里写是为了方便测试测试库,我们可以执行如下的测试命令:
1python LoginLibrary.py demo mode 2python LoginLibrary.py demo invalid
来验证我们的测试库编写是否正确,不过对于正式的测试库,我们需要进行单元测试utest和验收测试atest.这里就不做过多介绍了。编写好测试库以后,我们就可以利用测试库提供的关键字编写用例了。
测试用例
我们按照Robot框架的规范,编写测试用例login_tests.robot, 用例完整代码如下所示:
1*** Settings *** 2Library LoginLibrary.py 3 4*** Test Case *** 5Validate Users 6 [Template] Check Valid User 7 johns long 8 demo mode 9 10Login With Invalid User Should Fail 11 [Template] Check Invalid User 12 de mo 13 invalid invalid 14 long invalid 15 ${EMPTY} ${EMPTY} 16 17*** Keyword *** 18Check Valid User 19 [Arguments] ${username} ${password} 20 Check User ${username} ${password} 21 22Check Invalid User 23 [Arguments] ${username} ${password} 24 Run Keyword And Expect Error Wrong username/password combination Check User ${username} ${password}
我们的测试集包含了所有的测试情况,包括独立的有效登录测试和无效登录测试。 注意,尽管我们的测试用例文件以显示的.robot扩展结尾,它实际上也是文本文件。我们以.txt结尾,Robot框架同样可以执行。用例编写完了,我们开始执行测试用例。
执行测试
首先要确保已经安装了Robot Framework了,这里我就不介绍怎么安装了,相信大家应该都安装成功了。通过pybot --version查看即可。
我们在控制台上输入如下命令,执行测试:
>pybot login_tests.robot
pybot命令有很多参数可选,这里为了方便,我们就选用默认即可。
执行结果如下:
1============================================================================== 2Login Tests 3============================================================================== 4Validate Users | PASS | 5------------------------------------------------------------------------------ 6Login With Invalid User Should Fail | PASS | 7------------------------------------------------------------------------------ 8Login Tests | PASS | 92 critical tests, 2 passed, 0 failed 102 tests total, 2 passed, 0 failed 11============================================================================== 12Output: ../output.xml 13Log: ../log.html 14Report: ../report.html
通过查看输出的html日志和报告文件,我们可以查看用例的执行情况。
至此,我们就简单的介绍完了如何在Robot Framework测试库中调用C库的用法。