Robot Framework如何使用C语言编写测试库

简介: 介绍这里通过一个简单的例子,演示如何在Robot Framework 的测试库中使用C语言。我们使用Python标准库中的ctypes模块(对于早期Python版本可能未集成,需要另行安装),该模块需要调用C代码编写的共享库。

介绍

这里通过一个简单的例子,演示如何在Robot Framework 的测试库中使用C语言。我们使用Python标准库中的ctypes模块(对于早期Python版本可能未集成,需要另行安装),该模块需要调用C代码编写的共享库。当前的例子我仅在OSX上实现与测试,对于Unix和Linux平台大同小异,对于Windows平台,仅仅需要注意共享库的格式和调用方式即可,当前未做其他平台相关测试。

共享库

第一步,我们需要编写C的共享库。

我们编写的例子是一个非常简单的登录系统(login.c), 通过输入的用户名和密码进行验证,并返回验证结果。这里有两组合法的用户名密码组合:demo/mode和john/long.其他组合都是错误的。下面是login.c的完整代码:

/*
Simple system that validates passwords and user names. There are two users in
system with valid user name and password. "demo mode" and "john long". All
other user names are invalid. Except that there are bugs. Can you spot them?
*/

#include <string.h>
#define NR_USERS 2

struct User {
    const char* name;
    const char* password;
};
const struct User VALID_USERS[NR_USERS] = { "john", "long", "demo", "mode" };

int validate_user(const char* name, const char* password) {
    int i;
    for (i = 0; i < NR_USERS; i++) {
        if (0 == strncmp(VALID_USERS[i].name, name, strlen(VALID_USERS[i].name)))
            if (0 == strncmp(VALID_USERS[i].password, password,     strlen(VALID_USERS[i].password)))
                return 1;
    }
     return 0;
}

我们将这个文件编译成共享库liblogin.so. 在当前目录下,我们创建Makefile文件。
Makefile编写如下:

CC=gcc
SRC=login.c
SO=liblogin.so

$(SO): $(SRC)
    $(CC) -fPIC -shared -o $(SO) $(SRC)

clean:
    rm -f $(SO)

我们在当前目录执行make命令,就创建了共享库liblogin.so.
后面我们将介绍如何编写Robot Framework测试库来调用我们的C共享库。

测试库

在这里,我们按照Robot框架的规范,来编写测试库LoginLibrary.py. LoginLibrary是一个简单的测试库,通过ctypes模块来与底层的C共享库进行交互。我们这个库仅仅提供了一个关键字就是 Check User.

下面是LoginLibrary.py的完整代码:

"""Robot Framework test library example that calls C code.

This example uses Python's standard `ctypes` module, which requires
that the C code is compiled into a shared library.

It is also possible to execute this file from the command line 
to test the C code manually.
"""

from ctypes import CDLL, c_char_p

LIBRARY = CDLL('./liblogin.so')  # On Windows we'd use '.dll'


def check_user(username, password):
    """Validates user name and password using imported shared C library."""
    if not LIBRARY.validate_user(c_char_p(username), c_char_p(password)):
        raise AssertionError('Wrong username/password combination')


if __name__ == '__main__':
    import sys
    try:
        check_user(*sys.argv[1:])
    except TypeError:
        print 'Usage:  %s username password' % sys.argv[0]
    except AssertionError, err:
        print err
    else:
        print 'Valid password'

if __name__ == '__main__' 语句块不是用于测试库的,我们在这里写是为了方便测试测试库,我们可以执行如下的测试命令:

python LoginLibrary.py demo mode
python LoginLibrary.py demo invalid

来验证我们的测试库编写是否正确,不过对于正式的测试库,我们需要进行单元测试utest和验收测试atest.这里就不做过多介绍了。编写好测试库以后,我们就可以利用测试库提供的关键字编写用例了。

测试用例

我们按照Robot框架的规范,编写测试用例login_tests.robot, 用例完整代码如下所示:

*** Settings ***
Library           LoginLibrary.py

*** Test Case ***
Validate Users
    [Template]    Check Valid User
    johns    long
    demo     mode

Login With Invalid User Should Fail
    [Template]    Check Invalid User
    de          mo
    invalid     invalid
    long        invalid
    ${EMPTY}    ${EMPTY}

*** Keyword ***
Check Valid User
    [Arguments]    ${username}    ${password}
    Check User    ${username}    ${password}

Check Invalid User
    [Arguments]    ${username}    ${password}
    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命令有很多参数可选,这里为了方便,我们就选用默认即可。

执行结果如下:

==============================================================================
Login Tests                                                                   
==============================================================================
Validate Users                                                        | PASS |
------------------------------------------------------------------------------
Login With Invalid User Should Fail                                   | PASS |
------------------------------------------------------------------------------
Login Tests                                                           | PASS |
2 critical tests, 2 passed, 0 failed
2 tests total, 2 passed, 0 failed
==============================================================================
Output:  ../output.xml
Log:     ../log.html
Report:  ../report.html

通过查看输出的html日志和报告文件,我们可以查看用例的执行情况。

至此,我们就简单的介绍完了如何在Robot Framework测试库中调用C库的用法。

目录
相关文章
|
13天前
|
程序员 C语言
C语言库函数 — 内存函数(含模拟实现内存函数)
C语言库函数 — 内存函数(含模拟实现内存函数)
24 0
|
1月前
|
安全 程序员 C语言
探索C语言库函数:字符串拷贝函数strcpy
探索C语言库函数:字符串拷贝函数strcpy
19 0
|
1月前
|
C语言
模拟实现C语言中经典库函数,字符相关的函数与内存相关的函数
模拟实现C语言中经典库函数,字符相关的函数与内存相关的函数
模拟实现C语言中经典库函数,字符相关的函数与内存相关的函数
|
3月前
|
数据可视化 测试技术 持续交付
自动化测试神器:Python之Pytest库入门使用
自动化测试神器:Python之Pytest库入门使用
98 4
|
3月前
|
C++
jrtplib开源库系列之一:jrtplib介绍、安装和测试(window 10环境介绍)
关于jrtplib库网上已经有很多介绍,而且目前jrtplib作者已经停止更新(Apr 18, 2020),最新版本为v3.11.2。本系列内容也以该版本进行介绍。 相信你已经对RTP/RTCP协议有一定的了解,并想更深入的了解RTP协议的具体实现,jrtplib就是使用使用C++实现的RTP/RTCP协议。具体标准为RFC3550,如果想仔细阅读原文,但是对英文又有点吃力,可以参考我的博客RTP/RTCP中英文对照,在博客的后面有百度链接,是对RFC3550的中文翻译,可能很多地方不太准确,有些内容是自己添加进去的,希望不会影响你的阅读。
34 0
|
4月前
|
C语言
C语言转义字符第二篇和strlen库函数的使用
C语言转义字符第二篇和strlen库函数的使用
|
4月前
|
程序员 C语言
C语言什么是库函数 库函数怎么使用 为什么发明库函数
C语言什么是库函数 库函数怎么使用 为什么发明库函数
|
4月前
|
人工智能 算法 编译器
C语言初阶测评题:测试你的基础知识和编程技能!!
C语言初阶测评题:测试你的基础知识和编程技能!!
49 1
|
13天前
|
程序员 C语言 开发者
C语言库函数 — 字符串函数(含模拟实现字符串函数)
C语言库函数 — 字符串函数(含模拟实现字符串函数)
35 0
|
19天前
|
存储 程序员 编译器
【C语言第二回】main、printf和库函数
【C语言第二回】main、printf和库函数