Simplified doc-site generation
[AGL/documentation.git] / docs / 4_APIs_and_Services / 4.4_AGL_Test_Framework / 4_Tests_Examples / Tests_Examples.md
1 ---
2 edit_link: ''
3 title: Tests examples
4 origin_url: >-
5   https://git.automotivelinux.org/apps/app-afb-test/plain/docs/4_Tests_Examples.md?h=master
6 ---
7
8 <!-- WARNING: This file is generated by fetch_docs.js using /home/boron/Documents/AGL/docs-webtemplate/site/_data/tocs/apis_services/master/app-afb-test-developer-guides-api-services-book.yml -->
9
10 # Test example
11
12 To launch the binding use the command-line provided at the end of the build,
13 and the afb-daemon-demo just like in the example below. This will launch the
14 test of an Helloworld binding example. Tests's codes are available in the LUA
15 files `conf.d/controller/lua.d/helloworld.lua` and
16 `conf.d/controller/lua.d/aftTest.lua`.
17
18 The example will run some basics tests on API verb calls and events received.
19
20 ## helloworld.lua
21
22 ```lua
23     function _callback(responseJ)
24     _AFT.assertStrContains(responseJ.response, "Some String")
25     end
26
27     function _callbackError(responseJ)
28     _AFT.assertStrContains(responseJ.request.info, "Ping Binder Daemon fails")
29     end
30
31     function _callbackEvent(eventName, eventData)
32     _AFT.assertEquals(eventData, {data = { key = 'weird others data', another_key = 123.456 }})
33     end
34
35     _AFT.addEventToMonitor("hello/anEvent")
36     _AFT.addEventToMonitor("hello/anotherEvent", _callbackEvent)
37
38     _AFT.testVerbStatusSuccess('testPingSuccess','hello', 'ping', {})
39     _AFT.testVerbResponseEquals('testPingSuccess','hello', 'ping', {}, "Some String")
40     _AFT.testVerbResponseEquals('testPingSuccess','hello', 'ping', {}, "Unexpected String")
41     _AFT.testVerbCb('testPingSuccess','hello', 'ping', {}, _callback)
42     _AFT.testVerbStatusError('testPingError', 'hello', 'pingfail', {})
43     _AFT.testVerbResponseEqualsError('testPingError', 'hello', 'pingfail', {}, "Ping Binder Daemon fails")
44     _AFT.testVerbResponseEqualsError('testPingError', 'hello', 'pingfail', {}, "Ping Binder Daemon succeed")
45     _AFT.testVerbCbError('testPingError', 'hello', 'pingfail', {}, _callbackError)
46
47     _AFT.testVerbStatusSuccess('testEventAdd', 'hello', 'eventadd', {tag = 'event', name = 'anEvent'})
48     _AFT.testVerbStatusSuccess('testEventSub', 'hello', 'eventsub', {tag = 'event'})
49     _AFT.testVerbStatusSuccess('testEventPush', 'hello', 'eventpush', {tag = 'event', data = { key = 'some data', another_key = 123}})
50
51     _AFT.testVerbStatusSuccess('testEventAdd', 'hello', 'eventadd', {tag = 'evt', name = 'anotherEvent'})
52     _AFT.testVerbStatusSuccess('testEventSub', 'hello', 'eventsub', {tag = 'evt'})
53     _AFT.testVerbStatusSuccess('testEventPush', 'hello', 'eventpush', {tag = 'evt', data = { key = 'weird others data', another_key = 123.456}})
54
55     _AFT.testVerbStatusSkipped('testEventSub', 'hello', 'eventsub', {tag = 'evt'})
56
57     _AFT.testVerbStatusSuccess('testGenerateWarning', 'hello', 'verbose', {level = 4, message = 'My Warning message!'})
58
59     _AFT.testEvtGrpReceived("TestEventGroupReceived",{"hello/anEvent","hello/anotherEvent"},300000)
60     _AFT.testEvtGrpNotReceived("TestEventGroupNotReceived",{"hello/anEvent","hello/anotherEvent"},300000)
61
62     _AFT.testEvtReceived("testEvent", "hello/anEvent",300000)
63     _AFT.testEvtReceived("testEventCb", "hello/anotherEvent",300000)
64
65     _AFT.describe("myTestLabel", function()
66       _AFT.assertEquals(false, false)
67     end)
68 ```
69
70 ## aftTest.lua
71
72 ```lua
73 _AFT.setBeforeEach(function() print("~~~~~ Begin Test ~~~~~") end)
74 _AFT.setAfterEach(function() print("~~~~~ End Test ~~~~~") end)
75
76 _AFT.setBeforeAll(function() print("~~~~~~~~~~ BEGIN ALL TESTS ~~~~~~~~~~") return 0 end)
77 _AFT.setAfterAll(function() print("~~~~~~~~~~ END ALL TESTS ~~~~~~~~~~") return 0 end)
78
79
80 local corout = coroutine.create( print )
81
82 _AFT.describe("testAssertEquals", function() _AFT.assertEquals(false, false) end,
83                                   function() print("~~~~~ Begin Test Assert Equals ~~~~~") end,
84                                   function() print("~~~~~ End Test Assert Equals ~~~~~") end)
85
86 _AFT.describe("testAssertNotEquals", function()  _AFT.assertNotEquals(true,false) end)
87 _AFT.describe("testAssertItemsEquals", function()  _AFT.assertItemsEquals({1,2,3},{3,1,2}) end)
88 _AFT.describe("testAssertAlmostEquals", function()  _AFT.assertAlmostEquals(1.25 ,1.5,0.5) end)
89 _AFT.describe("testAssertNotAlmostEquals", function()  _AFT.assertNotAlmostEquals(1.25,1.5,0.125) end)
90 _AFT.describe("testAssertEvalToTrue", function()  _AFT.assertEvalToTrue(true) end)
91 _AFT.describe("testAssertEvalToFalse", function()  _AFT.assertEvalToFalse(false) end)
92
93 _AFT.describe("testAssertStrContains", function()  _AFT.assertStrContains("Hello I'm a string","string") end)
94 _AFT.describe("testAssertStrContains", function()  _AFT.assertStrContains("Hello I'm a second string","second",5) end)
95
96 _AFT.describe("testAssertStrIContains", function()  _AFT.assertStrIContains("Hello I'm another string","I'm") end)
97
98 _AFT.describe("testAssertNotStrContains", function()  _AFT.assertNotStrContains("Hello it's me again, the other string","banana") end)
99 _AFT.describe("testAssertNotStrContains", function()  _AFT.assertNotStrContains("Hello it's me again, the other string","banana",8) end)
100 ...
101 ...
102 ...
103 _AFT.describe("testAssertNotIsUserdata", function()  _AFT.assertNotIsUserdata(2) end)
104
105
106 function _callback(responseJ) _AFT.assertStrContains(responseJ.response, "Some String") end
107 function _callbackError(responseJ) _AFT.assertStrContains(responseJ.request.info, "Ping Binder Daemon fails") end
108
109 _AFT.describe("testAssertVerbStatusSuccess",function() _AFT.assertVerbStatusSuccess('hello', 'ping', {}) end)
110 _AFT.describe("testAssertVerbStatusSkipped",function() _AFT.assertVerbStatusSkipped('hello', 'ping', {}) end)
111 _AFT.describe("testAssertVerbResponseEquals",function() _AFT.assertVerbResponseEquals('hello', 'ping', {},"Some String") end)
112 _AFT.describe("testAssertVerbCb",function() _AFT.assertVerbCb('hello', 'ping', {},_callback) end)
113 _AFT.describe("testAssertVerbStatusError",function() _AFT.assertVerbStatusError('hello', 'pingfail', {}) end)
114 _AFT.describe("testAssertVerbResponseEqualsError",function() _AFT.assertVerbResponseEqualsError('hello', 'pingfail', {},"Ping Binder Daemon fails") end)
115 _AFT.describe("testAssertVerbCbError",function() _AFT.assertVerbCbError('hello', 'pingfail', {},_callbackError) end)
116 ```
117
118 > **NOTE**: I suggest you to take this lua file example to make your own test
119 > then read the following chapter if needed to write more complex tests.