mirror of
https://github.com/LCTT/TranslateProject.git
synced 2024-12-26 21:30:55 +08:00
" 21070711 Functional testing Gtk+ applications in C " translated by sugarfillet (#6122)
* translated by sugarfillet 20170711 * Update 20170711 Functional testing Gtk applications in C.md
This commit is contained in:
parent
9ecb10eb9d
commit
07e3873238
@ -1,23 +1,22 @@
|
||||
translating by sugarfillet
|
||||
Functional testing Gtk+ applications in C
|
||||
============================================================
|
||||
C 语言对 Gtk+ 应用进行功能测试
|
||||
========
|
||||
### 这个简单教程教你如何测试你应用的功能
|
||||
|
||||
### Learn how to test your application's function with this simple tutorial.
|
||||
![Functional testing Gtk+ applications in C](https://opensource.com/sites/default/files/styles/image-full-size/public/images/business/cube_innovation_block_collaboration.png?itok=CbG3Mpqi "Functional testing Gtk+ applications in C ")
|
||||
|
||||
|
||||
![Functional testing Gtk+ applications in C ](https://opensource.com/sites/default/files/styles/image-full-size/public/images/business/cube_innovation_block_collaboration.png?itok=CbG3Mpqi "Functional testing Gtk+ applications in C ")
|
||||
Image by :
|
||||
图片源自 :
|
||||
|
||||
opensource.com
|
||||
|
||||
Automated tests are required to ensure your program's quality and that it works as expected. Unit tests examine only certain parts of your algorithm, but don't look at how each component fits together. That's where functional testing, sometimes referred as integration testing, comes in.
|
||||
|
||||
A functional test basically interacts with your user interface, whether through a website or a desktop application. To show you how that works, let's look at how to test a Gtk+ application. For simplicity, in this tutorial let's use the [Tictactoe][6] example from the Gtk+ 2.0 tutorial.
|
||||
自动化测试用来保证你程序的质量以及让它以预想的运行。单元测试只是检测你算法的某一部分,但是并不注重各组件间的适应性。这就是为什么会有功能测试,有时也称为集成测试。
|
||||
|
||||
### Basic setup
|
||||
|
||||
For every functional test, you usually define some global variables, such as "user interaction delay" or "timeout until a failure is indicated" (i.e., when an event doesn't occur until the specified time and the application is doomed).
|
||||
一个功能测试简单地与你的用户界面交互,可通过一个网站或者一个桌面应用。为了展示功能测试如何工作,我们以测试一个 Gtk+ 应用为例。为了简单,这个教程里,我们使用 Gtk+ 2.0 教程的示例。
|
||||
|
||||
### 基础设置
|
||||
|
||||
每一个功能测试,你通常需要定义一些全局变量,比如 “用户交互时延” 或者 “失败的超时时间”(也就是说,如果在指定的时间内一个时间没有发生,程序就要中断)。
|
||||
```
|
||||
#define TTT_FUNCTIONAL_TEST_UTIL_IDLE_CONDITION(f) ((TttFunctionalTestUtilIdleCondition)(f))
|
||||
#define TTT_FUNCTIONAL_TEST_UTIL_REACTION_TIME (125000)
|
||||
@ -29,8 +28,7 @@ struct timespec ttt_functional_test_util_default_timeout = {
|
||||
};
|
||||
```
|
||||
|
||||
Now we can implement our dead-time functions. Here, we'll use the **usleep** function in order to get the desired delay.
|
||||
|
||||
现在我们可以实现我们自己的超时函数。这里,为了能够得到期望的延迟,我们采用 **usleep** 函数。
|
||||
```
|
||||
void
|
||||
ttt_functional_test_util_reaction_time()
|
||||
@ -45,7 +43,7 @@ ttt_functional_test_util_reaction_time_long()
|
||||
}
|
||||
```
|
||||
|
||||
The timeout function delays execution until a state of a control is applied. It is useful for actions that are applied asynchronously, and that is why it delays for a longer period of time.
|
||||
直到控制状态被执行,超时函数才会推迟执行。这对于一个异步执行的动作很有帮助,这也是为什么采用这么长的时延。
|
||||
|
||||
```
|
||||
void
|
||||
@ -74,9 +72,10 @@ ttt_functional_test_util_idle_condition_and_timeout(
|
||||
}
|
||||
```
|
||||
|
||||
### Interacting with the graphical user interface
|
||||
### 与图形化用户界面交互
|
||||
|
||||
In order to simulate user interaction, the [**Gdk library**][7] provides the functions we need. To do our work here, we need only these three functions:
|
||||
|
||||
为了模拟用户交互的操作, [**Gdk library**][7] 提供一些我们需要的函数。为了完成我们的工作,我们只需要如下 3 个函数。
|
||||
|
||||
* gdk_display_warp_pointer()
|
||||
|
||||
@ -84,8 +83,8 @@ In order to simulate user interaction, the [**Gdk library**][7] provides the f
|
||||
|
||||
* gdk_test_simulate_key()
|
||||
|
||||
For instance, to test a button click, we do the following:
|
||||
|
||||
举个例子,为了测试按钮点击,我们可以这么做:
|
||||
```
|
||||
gboolean
|
||||
ttt_functional_test_util_button_click(GtkButton *button)
|
||||
@ -151,7 +150,8 @@ ttt_functional_test_util_button_click(GtkButton *button)
|
||||
}
|
||||
```
|
||||
|
||||
We want to ensure the button has an active state, so we provide an idle-condition function:
|
||||
|
||||
我们想要保证按钮处于激活状态,因此我们提供一个空闲条件函数:
|
||||
|
||||
```
|
||||
gboolean
|
||||
@ -176,12 +176,12 @@ ttt_functional_test_util_idle_test_toggle_active(
|
||||
}
|
||||
```
|
||||
|
||||
### The test scenario
|
||||
|
||||
Since the Tictactoe program is very simple, we just need to ensure that a [**GtkToggleButton**][8] was clicked. The functional test can proceed once it asserts the button entered the active state. To click the buttons, we use the handy **util** function provided above.
|
||||
### 测试场景
|
||||
|
||||
For illustration, let's assume player A wins immediately by filling the very first row, because player B is not paying attention and just filled the second row:
|
||||
因为 Tictactoe 程序非常简单,我们只需要保证一个 [**GtkToggleButton**][8] 被点击。一旦说按钮肯定进入了激活状态,功能测试就可以执行。为了点击按钮,我们使用上面提到的手动的 **工具** 。
|
||||
|
||||
如图所示,我们假设,填满第一行,玩家 A 就赢,因为玩家 B 没有注意,只填充了第二行。
|
||||
```
|
||||
GtkWindow *window;
|
||||
Tictactoe *ttt;
|
||||
@ -269,13 +269,13 @@ main(int argc, char **argv)
|
||||
|
||||
作者简介:
|
||||
|
||||
Joël Krähemann - Free software enthusiast with a strong knowledge about the C programming language. I don't fear any code complexity as long it is written in a simple manner. As developer of Advanced Gtk+ Sequencer I know how challenging multi-threaded applications can be and with it we have a great basis for future demands.my personal website
|
||||
|
||||
Joël Krähemann - 精通 C 语言编程的自由软件爱好者。我不怕代码有多复杂,它只是以一种简单的方法去编码。作为高级的 Gtk+ 序的开发者,我知道多线程编程有着多大的挑战性,有了多线程编程,我们就有了未来需求的良好基础。
|
||||
|
||||
via: https://opensource.com/article/17/7/functional-testing
|
||||
摘自: https://opensource.com/article/17/7/functional-testing
|
||||
|
||||
作者:[Joël Krähemann][a]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
译者:[sugarfillet](https://github.com/sugarfillet)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
Loading…
Reference in New Issue
Block a user