You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by ad...@apache.org on 2016/03/14 07:37:24 UTC

[2/5] incubator-mynewt-site git commit: final doc changes for documenting building test libraries

final doc changes for documenting building test libraries


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-site/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-site/commit/1ccf772f
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-site/tree/1ccf772f
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-site/diff/1ccf772f

Branch: refs/heads/master
Commit: 1ccf772f71748f3a73c667dc9123acebb305c693
Parents: 2bb96ab
Author: Paul Dietrich <pa...@yahoo.com>
Authored: Fri Mar 11 12:59:33 2016 -0800
Committer: Paul Dietrich <pa...@yahoo.com>
Committed: Fri Mar 11 12:59:33 2016 -0800

----------------------------------------------------------------------
 docs/os/modules/testutil/test_case.md |   2 +-
 docs/os/modules/testutil/test_decl.md |  28 +++++
 docs/os/modules/testutil/testutil.md  |  31 +----
 docs/os/tutorials/unit_test.md        | 192 ++++++++++++++++++++++++++++-
 mkdocs.yml                            |   1 +
 5 files changed, 225 insertions(+), 29 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-site/blob/1ccf772f/docs/os/modules/testutil/test_case.md
----------------------------------------------------------------------
diff --git a/docs/os/modules/testutil/test_case.md b/docs/os/modules/testutil/test_case.md
index 61a7aef..24bfd38 100644
--- a/docs/os/modules/testutil/test_case.md
+++ b/docs/os/modules/testutil/test_case.md
@@ -4,7 +4,7 @@
 TEST_CASE(test_case_name)
 ```
 
-Declares a test case function with the following type `int test_case_name(void)`. This can then be called from regression test's `TEST_SUITE()` function.
+Defines a test case function with the following type `int test_case_name(void)`. This can then be called from regression test's `TEST_SUITE()` function.
 
 #### Arguments
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-site/blob/1ccf772f/docs/os/modules/testutil/test_decl.md
----------------------------------------------------------------------
diff --git a/docs/os/modules/testutil/test_decl.md b/docs/os/modules/testutil/test_decl.md
new file mode 100644
index 0000000..a581ea5
--- /dev/null
+++ b/docs/os/modules/testutil/test_decl.md
@@ -0,0 +1,28 @@
+## <font color="F2853F" style="font-size:24pt"> TEST_CASE_DECL </font>
+
+```no-highlight
+TEST_CASE_DECL(test_case_name)
+```
+
+Declares a test case function with the following type `int test_case_name(void)`. This can then be called from regression test's `TEST_SUITE()` function.  This is only required if the test case function 
+exists in a different file than the test suite.  This will allow the test suite
+to find the test case
+
+#### Arguments
+
+| Arguments | Description |
+|-----------|-------------|
+| test_case_name | Used as the function name for this test case. |
+
+#### Returned values
+
+Return value is 0 if the test case passed; nonzero if it failed. Generally, the return code is not used. It is expected that the case will pass/fail with tests done using `TEST_ASSERT()`.
+
+
+#### Example file `test_cases.h`
+
+```no-highlight
+TEST_CASE_DECL(test_case_1)
+TEST_CASE_DECL(test_case_2)
+TEST_CASE_DECL(test_case_3)
+```

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-site/blob/1ccf772f/docs/os/modules/testutil/testutil.md
----------------------------------------------------------------------
diff --git a/docs/os/modules/testutil/testutil.md b/docs/os/modules/testutil/testutil.md
index 50ef5a0..be6d3e9 100644
--- a/docs/os/modules/testutil/testutil.md
+++ b/docs/os/modules/testutil/testutil.md
@@ -46,32 +46,8 @@ Regression test can then be executed using 'newt target test' command, or by inc
 
 ### Example
 
-The following example demonstrates how to create a simple test suite.
-
-```no-highlight
-    TEST_CASE(test_addition)
-    {
-        int sum;
-
-        sum = 5 + 10;
-        TEST_ASSERT(sum == 15, "actual value: %d", sum);
-    }
-
-    TEST_CASE(test_times_0)
-    {
-        TEST_ASSERT(3 * 0 == 0);
-        TEST_ASSERT(4 * 0 == 0);
-        TEST_ASSERT(712 * 0 == 0);
-    }
-
-    TEST_SUITE(test_suite_arithmetic)
-    {
-        test_addition();
-        test_times_0();
-    }
-```
-
-The test suite would then be executed via a call to `test_suite_arithmetic()`.
+[This Tutorial](../../tutorials/unit_test.md) shows how to create a test suite
+for a Mynewt package.
 
 ###Data structures
 
@@ -104,11 +80,12 @@ This should be populated before `tu_init()` is called.
 
 <Comments such as these instructions are placed within angle brackets. List all the functions here. Note how the anchors work. You put the text you want to show up as a link within [] and the relevant #heading within (). Note that the header has to have at least 2 words for the anchor to work - that's how it is. "no-highlight" disables syntax highlighting. You can enable it for a particular language by specifying what the language is instead of "no-highlight". Be warned that this highlighting or no-highlighting specification may not show up nicely on Mou.>
 
-The functions, and macros available in testutil are:
+The functions, and macros available in `testutil` are:
 
 * [tu_init](tu_init.md)
 * [TEST_ASSERT](test_assert.md)
 * [TEST_PASS](test_pass.md)
 * [TEST_SUITE](test_suite.md)
 * [TEST_CASE](test_case.md)
+* [TEST_CASE_DECL](test_decl.md)
 * [tu_restart](tu_restart.md)

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-site/blob/1ccf772f/docs/os/tutorials/unit_test.md
----------------------------------------------------------------------
diff --git a/docs/os/tutorials/unit_test.md b/docs/os/tutorials/unit_test.md
index 43e6a59..008bc9e 100644
--- a/docs/os/tutorials/unit_test.md
+++ b/docs/os/tutorials/unit_test.md
@@ -1 +1,191 @@
-## How to Test an Egg
+# How To Write A Test Suite for Your Package
+
+This document presents a tutorial which guides the reader through writing
+a test suite for a Mynewt package (new or existing).
+
+## Introduction
+
+Writing a test suite involves using the [`libs/testutil`](../modules/testutil/testutil.md)
+ package within Mynewt core os. The `testutil` library provides the interface to 
+the `newt` command tool and also provides the compile time hooks to include
+test suites into your code.  Review the 
+[`testutil` introduction page ] (../modules/testutil/testutil.md)
+to learn about how the library works.
+
+### Identify Your Package
+
+Identify the package for which you are writing a test suite.  For this example
+we will use `libs/json`.  To create a new package, see [this Tutorial]().
+
+### Modify Pkg.yml
+
+Edit the package (`pkg.yml`) file for your package and add the test dependency
+for the Mynewt core OS `libs/testutil`.
+
+```no-highlight
+pkg.deps.TEST:
+   - libs/testutil
+```
+
+### Create Your Test Suite Template
+
+Create a subdirectory `test` under your package main directory. 
+Create a file pair for your test code and header files within the `test`
+directory created above.  Below shows the `libs/json` directory within the 
+Mynewt core, including the test directory. In this example, we used the 
+convention `test_xxx.c/h` (in this case `test_json`).
+
+```no-highlight
+├── MSJSON_COPYING
+├── include
+│   └── json
+│       └── json.h
+├── pkg.yml
+└── src
+    ├── json_decode.c
+    ├── json_encode.c
+    └── test
+        ├── test_json.c
+        └── test_json.h
+```
+
+### Create Your Test Suite Code 
+
+Edit the `test_json.c` file and add your test suite definition.  NOTE that 
+the test suite code requires `#include <testutil/testutil.h>` to get the 
+Mynewt testutil definitions.
+
+Your test suite `test_json.c` file contains at a minimum two functions
+
+1. A test Suite which is empty for now, but will contain calls to your test
+cases.  
+2. A main function which must be `#ifdef`'d using `MYNEWT_SELFTEST` to ensure
+that is does not get compiled in when this test suite is run with 
+test suites from other packages 
+
+Below shows the contents of the `test_json.c` file.
+
+```no-highlight
+#include <testutil/testutil.h>
+
+TEST_SUITE(test_json_suite) {
+    /* empty for now, add test cases later */
+}
+
+#ifdef MYNEWT_SELFTEST
+int
+main(int argc, char **argv)
+{
+    tu_config.tc_print_results = 1;
+    tu_init();
+    test_json_suite();
+    return tu_any_failed;
+}
+#endif
+```
+### Try It Out
+
+At this point, you have a working test suite with __no__ tests.  
+This will by default pass the test.  Your output will look
+something like this.
+
+You can use the `newt test` command to run the unit tests for any package.  
+Just include the package name.  These unit tests run via the project 
+`unittest` which is a native project automatically included in the core
+os package.  Below shows some of the test output of this command.
+
+```no-highlight
+$ newt test libs/json
+Archiving util.a
+Linking test_json
+Testing package libs/json
+Test .../bin/unittest/libs/json/test_json ok!
+```
+### Create a Test 
+
+To create a test within your test suite, there are two things to do.
+
+1. Add the functions to your test suite
+2. Implement the function using the `testutil` macros
+
+For this tutorial we will create two functions: one to test a simple json
+encode and one to test the decode of this simple message to ensure its 
+coherent.
+
+Follow These steps;
+
+1\. Create function prototypes in `test_json.h` for your test functions. 
+A macro in `testutil.h` hides the actual prototype, but as of this writing
+the prototype is `int test_func(void);`.     
+```no-highlight
+#ifndef TEST_JSON_H
+#define TEST_JSON_H
+
+TEST_CASE_DECL(test_json_simple_encode);
+TEST_CASE_DECL(test_json_simple_decode);
+
+#endif /* TEST_JSON_H 
+```
+2\. create a new file `test_json_simple.c` to define these two functions.  For
+now you can stub these functions. Below shows the contents of this file. 
+The functions are defined using macros which reference back to the 
+`testutil` library so the test can be enumerated and recorded automatically.
+```no-highlight
+#include "testutil/testutil.h"
+#include "test_json.h"
+
+TEST_CASE(test_json_simple_encode) {
+}
+
+TEST_CASE(test_json_simple_decode) {
+}
+#endif /* TEST_JSON_H 
+```
+3\. Add the tests to your test suite in `test_json.c` as shown below.
+```no-highlight
+TEST_SUITE(test_json_suite) {
+    test_json_simple_encode();
+    test_json_simple_decode();
+}
+```
+
+Your test suite should still pass as shown below
+
+```no-highlight
+$newt test libs/json
+Testing package libs/json
+Test .../bin/unittest/libs/json/test_json ok!
+```
+
+## Add Contents to your Tests
+
+At this point, you can add contents to your test and verify that 
+the test suites pass.  For now, lets just add a simple failures to show
+what it would look like when running from Newt.
+
+1. Edit `test_json_simple.c` and add a `TEST_ASSERT` to a test funtion. The
+test assert will fail if its argument is `false`.
+
+
+```no-highlight
+TEST_CASE(test_json_simple_encode) {
+    TEST_ASSERT(0);
+}
+```
+When running newt, you will see the test suite failures something like
+the message shown below.
+
+```no-highlight
+Testing package libs/json
+[FAIL] test_json_suite/(null) |test_json_simple.c:24| failed assertion: 0
+Error: Test crashed: ../bin/unittest/libs/json/test_json
+exit status 1
+```
+
+## Congratulations
+
+Now you can begin the work of adding your test cases and test 
+
+
+## Testing on your target
+

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-site/blob/1ccf772f/mkdocs.yml
----------------------------------------------------------------------
diff --git a/mkdocs.yml b/mkdocs.yml
index f3e0b49..178750e 100644
--- a/mkdocs.yml
+++ b/mkdocs.yml
@@ -256,6 +256,7 @@ pages:
                 - 'TEST_PASS': 'os/modules/testutil/test_pass.md'
                 - 'TEST_SUITE': 'os/modules/testutil/test_suite.md'
                 - 'TEST_CASE': 'os/modules/testutil/test_case.md'
+                - 'TEST_CASE_DECL': 'os/modules/testutil/test_decl.md'
                 - 'tu_restart': 'os/modules/testutil/tu_restart.md'
         - Image Manager:
             - toc: 'os/modules/imgmgr/imgmgr.md'