Revision: 2364
Author: KariHusa
Date: Fri Oct 23 02:24:32 2009
Log: initial content
http://code.google.com/p/robotframework/source/detail?r=2364
Added:
/wiki/UsingC.wiki
=======================================
--- /dev/null
+++ /wiki/UsingC.wiki Fri Oct 23 02:24:32 2009
@@ -0,0 +1,114 @@
+#summary Exaplme of using C from Robot Framework test libraries.
+
+= Using C from Robot Framework test libraries =
+
+This simple example demonstrates how to use C language from Robot
+Framework test libraries. The example uses Python's standard
[http://docs.python.org/library/ctypes.html ctypes module], which requires
+that the C code is compiled into a shared library. This version is
+implemented and tested on Linux, but adapting it to other operating
+systems would require only changing compilation and name of the
+produced shared library.
+
+== System under test ==
+
+The demo application is simple login system that validates user name
+and password and prints out are they valid or not. There are two valid
+username password combinations: `demo/mode` and `john/long`.
+
+{{{
+#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;
+}
+}}}
+
+
+== Test library ==
+
+A simple test library that can execute the above program directly using
`ctypes`.
+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'
+}}}
+
+== Test cases ==
+
+|| *** Settings *** || || || ||
+|| Library || LoginLibrary.py || || ||
+|| || || || ||
+|| *** Test Case *** || || || ||
+|| Validate Users || || || ||
+|| || Check Valid User || john || long ||
+|| || Check Valid User || demo || mode ||
+|| || || || ||
+|| Login With Invalid User Should Fail || || || ||
+|| || Check Invalid User || de || mo ||
+|| || Check Invalid User || invalid || invalid ||
+|| || Check Invalid User || long || invalid ||
+|| || Check Invalid User || ${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} ||
+
+
+
+
+
+The example consists of four files:
+
+ * `login.c`: The demo application under test
+ * `LoginLibrary.py`: Test library that interacts with the demo
application
+ * `LoginTests.tsv`: Example tests
+ * `Makefile`: Used to compile the demo application
+
+To use this example run `make` in the directory where you unzipped the
+`robotframework-c-example.zip` package. This will create library
+`liblogin.so`, a shared library that is needed to use ctypes
+module. Run test by typing:
+
+ pybot LoginTests.tsv
+
+You can also run the application as standalone using command:
+
+ python LoginLibrary.py demo mode