SlideShare uma empresa Scribd logo
1 de 45
Baixar para ler offline
TDD with Python unittest
          for
     embedded C

            Ben6
           2012-08-28


        www.juluos.org
Agenda
● TDD是什麼?

● 為什麼使用TDD?

● python unittest
  ○ C & Embedded C
● Real Practice
 1. BOS 實際導入
 2. Dummy test and LED Driver
TDD是什麼?
What's TDD?


              測試先行




      意思就是,在開發程式時,
      要以測試的角度來設計。
Red   1. Write a test that fails

       先寫一個測試結果錯誤




TDD
Red            1. Write a test that fails

                  先寫一個測試結果錯誤




TDD
2. Make the code work
                                Green
      使程式可用
Red            1. Write a test that fails

                                    先寫一個測試結果錯誤




3. Eliminate
redundancy
                  TDD
消除冗餘

                  2. Make the code work
    Refactoring                                 Green
                        使程式可用
Red            1. Write a test that fails

                                                        先寫一個測試結果錯誤




3. Eliminate redundancy
                                   TDD
   消除冗餘

                                      2. Make the code work
         Refactoring                                                Green
                                            使程式可用

                          TDD 的開發節奏 "Red, Green, Refactoring"
TDD的觀點


All code is guilty until proven innocent.



任何代碼都是有問題的,直到證明他無誤。
重構 (Refactoring)

Refactoring 是重構一詞,英文以現在進行式,意
味,重構應該不間斷地持續進行。

三項關鍵技能
● 對壞代碼的嗅覺
● 對更好代碼的遠見
● 轉化代碼
請問你有使用TDD嗎?


若有,為什麼使用?
我為什麼使用TDD?


 為了不浪費生命在重複手動測試的事務上

為了有自信重構程式使程式碼更易維護及理解

為了利用測試腳本將產品規格更清除的規範

        更多...
Python unittest
Python unittest
                  $ python sample.py
                  setUp()
                  testCase1()
                  tearDown()
                  .setUp()
                  testCase2()
                  tearDown()
                  .
                  -----------------------------------------
                  -----------------------------
                  Ran 2 tests in 0.000s

                  OK
Python unittest
import random
import unittest

class TestSequenceFunctions(unittest.TestCase):

  def setUp(self):
    self.seq = range(10)

  def test_shuffle(self):
    random.shuffle(self.seq)
    self.seq.sort()
    self.assertEqual(self.seq, range(10))

    self.assertRaises(TypeError, random.shuffle, (1,2,3))

  def test_choice(self):
    element = random.choice(self.seq)
    self.assertTrue(element in self.seq)

  def test_sample(self):
    with self.assertRaises(ValueError):
       random.sample(self.seq, 20)
    for element in random.sample(self.seq, 5):
       self.assertTrue(element in self.seq)

if __name__ == '__main__':
   unittest.main()
Python 與 C

c library: libfoo.so

 int foo(unsigned int r);

python ctypes sample

 from ctypes import *
 foolib = CDLL.LoadLibrary('libfoo.so')
 r = foolib.foo(c_uint(5))
Embedded C

Definitions
● Related to hardware environment


● Ex: A driver
    ■   LED
    ■   Netowrk Interface Card
    ■   VGA Card
    ■   self test
    ■   ...
ctypes
ctypes type    C type                                   Python type
c_bool         _Bool                                    bool (1)
c_char         char                                     1-character string
c_wchar        wchar_t                                  1-character unicode string
c_byte         char                                     int/long
c_ubyte        unsigned char                            int/long
c_short        short                                    int/long
c_ushort       unsigned short                           int/long
c_int          int                                      int/long
c_uint         unsigned int                             int/long
c_long         long                                     int/long
c_ulong        unsigned long                            int/long
c_longlong     __int64 or long long                     int/long
c_ulonglong    unsigned __int64 or unsigned long long   int/long
c_float        float                                    float
c_double       double                                   float
c_longdouble   long double                              float
c_char_p       char * (NUL terminated)                  string or None
c_wchar_p      wchar_t * (NUL terminated)               unicode or None
c_void_p       void *                                   int/long or None
sample ctypes

>>> from ctypes import *
>>> p = create_string_buffer(3)
>>> print sizeof(p), repr(p.raw)
3 'x00x00x00'

>>> p = create_string_buffer("Hello")
>>> print sizeof(p), repr(p.raw)
6 'Hellox00'

>>> print repr(p.value)
'Hello'
sample ctypes


>>> from ctypes import *
>>> p = create_string_buffer("Hello", 10)
>>> print sizeof(p), repr(p.raw)
10 'Hellox00x00x00x00x00'

>>> p.value = "Hi"
>>> print sizeof(p), repr(p.raw)
10 'Hix00lox00x00x00x00x00'
python cdll in different platform


 from ctypes import *
 import sys

 platform = sys.platform
 if sys.platform == "cygwin":
     libc = cdll.LoadLibrary("/bin/cygwin1.dll")
 else:
     libc = CDLL('libc.so.6')
python cdll in different platform (cont.)


 import sys
 from math import log

 def is64bit():
   return log(sys.maxsize, 2) == 63

 arch=32
  if is64bit():
     arch = 64
案例研討(一)

     BOS 實際導入

blibc pytest @ github
CFILES= ../../blibc/itoa.c
OBJS=$(CFILES:.c=.o)
CFLAGS= -I ../../include            Makefile for
BLIBC_SHARED = libbosc.so          unittest blibc
all: $(BLIBC_SHARED)
      python alltests.py

%.o :%.c
     $(CC) -c $< -o $@ $(CFLAGS)

$(BLIBC_SHARED): $(OBJS)
    $(CC) -shared -o $@ $^

clean:
           rm -f $(BLIBC_SHARED) $(OBJS)
import unittest,os
from ctypes import *
app_path = os.path.dirname(os.path.abspath(__file__))
libpathname = os.path.join(app_path, "./libbosc.so")
bc = CDLL(libpathname);
class BOSTest_atoi(unittest.TestCase):
   s = (c_byte*9)()
   c = 427
   def test_atoi(self):                            alltests.py
      # ... next page

def suite_blibc():
  bosTestSuite = unittest.makeSuite(BOSTest_atoi, 'test')
  return bosTestSuite

def main():
  suite1 = suite_blibc()
  alltests = unittest.TestSuite((suite1))
  runner = unittest.TextTestRunner()
  runner.run(alltests);
def test_atoi(self):            test 1 in alltests.py
 # const char *itohex(uint32_t c, char *s, int size, int upper)
 b = bc.itohex(self.c, self.s, c_int(9), c_int(0))
 hex_str = string_at(self.s)
 assert hex_str == "000001ab", "atoi padding hex string"
 assert string_at(b) == "1ab"," atoi incorrect no-zero hex
padding"


                              test 2 in alltests.py
 def test_auto_with_upper(self):
  upper_hex = bc.itohex(self.c, self.s, c_int(9), c_int(1))
  assert string_at(upper_hex) == "1AB", 
    " atoi incorrect no-zero upper hex padding"
實戰演練(一)

Dummy LED(s) Driver
實戰演練(一)發生錯誤的單元測試
 空的測試腳本包含:
   ●      a dummy python test (alltests.py)
           ○ use python ctypes to load lib function
   ●      a sample Makefile to create shared library for python unitest

git clone -b tdd_leds_sample git@github.com:benwei/JuluOS.git tdd_leds
$ cd tdd_leds/tests
$ git checkout -b your_tdd1 a325e85366da5d0d3735863aa983a27700829a28

$ make
python alltests.py
E
======================================================================
ERROR: test_turn_onoff (__main__.BOSTest_led)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "alltests.py", line 21, in test_turn_onoff
   led.turn_on(c_char(0))
TypeError: one character string expected

----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (errors=1)
實戰演練(一)使程式通過測試

  1. 建立 ../drivers/led.c
  2. 並使之通過測試


結果如下:
$ make
cc -c ../drivers/led.c -o ../drivers/led.o -I ../inc
cc -shared -o libled.so ../drivers/led.o
python alltests.py
after turnon 1
after turnoff 0
.
----------------------------------------------------------------------
Ran 1 test in 0.001s

OK
實戰演練(二)
    Refactoring
        及
TDD使用Mock LED(s) Driver
實際演練(二)LED Driver with TDD

Make a test that fails
● Create dummy test function
● Create a Mock LED Device for monitoring
● Add LED turn_on/off api to test function
● Check expect result of LED state

Make code work
● write the operation code for LED

Refactoring
● refined wording, duplicated code, naming ...
Mock LED(s) Device
● 可是一個記憶體位置
● 可為表示狀態的指示器
● 一個簡化後的虛擬裝置

Ex:
uint mock_led = 0;
led_plug(&mock_led, id_1);

     Mock Object 在開發過程十分重要,因為當測試環境愈單純,將會更易於自
 !   動化測試的建構。http://en.wikipedia.org/wiki/Mock_object
實際演練(二)

● 練習題
   ○ 擴充為十個LED 燈,可以同時點亮,也可單獨開關其中
     之一。


完成後原始碼:
$ git checkout tdd_leds_sample
$ git checkout 453b80514da4793f6e608343742ba89bb870dcd6
實際演練(二)TDD完成輸入畫面
git clone -b tdd_leds_sample git@github.com:benwei/JuluOS.git tdd_leds
$ cd tdd_leds/tests
tests$ ls
alltests.py Makefile
tests$ make
cc -c ../drivers/leds.c -o ../drivers/leds.o -I ../inc
cc -shared -o libleds.so ../drivers/leds.o
python alltests.py
after turnon 1
..
----------------------------------------------------------------------
Ran 2 tests in 0.000s

OK
實際演練(二)延伸練習

● 使用者可由外部傳入LED Mock Objects

● 使用者可使用不同顏色LED

● 使用者可設任一LED燈亮1~10秒

 ○ 使用python + timer check 來確定其執行時間
Conclusion
TDD 好處

            較少的bug

         減少手動重複測試的時間

            文件檔案

容易改善設計,因不將設計的耦合降低,將無法進行單元測試

  根據測式程式後的記錄,能很清楚知道目前的進度。

   很關卡遊戲,設置問題,填入解答(可不斷精鍊)
        ,獲得完成時的成就感。
1.Write a test
                      that fails




                     TDD
 Eliminate                                      2. Make
redundancy                                     the code
                                                 work

       The mantra of TDD is "Red, green, refactoring"
Ending - Test-driven development
        a programming technique that
       requires you to write actual code
    automated test code simultaneously




        ensures you test your code
 enables you to retest your code quickly and
         easily, since it’s automated
Q&A?
                     Maintain
  write a
 test fails
              TDD?


   Refactor     duplicate
Backlogs
References
Mock Objects for TDD
●   Why and When to Use Mock Objects
●   library unittest mock
●   wikipedia.org/wiki/Mock_object
How python with cli?
cli - interface
                                        ?
● input via arguments

● cli output
   ○ update the specific files
   ○ exit code
   ○ console output


● use unittest program testing cli output
ctypes use global variable in lib
led = CDLL("./libled.so")

v = c_uint.in_dll(led, "_led")
print v.value()

Reference:
http://docs.python.org/library/ctypes.
html#accessing-values-exported-from-dlls
  !   筆者的意見:儘量不要直接存取全域變數;經由封裝函數介面來使用,
             如:uint get_led(void) { return _led; }
loremipsum
A Lorem Ipsum text generator

    pypi.python.org/pypi/loremipsum/1.0.2
>>> from loremipsum import Generator
>>>
>>> sample = file('data/sample.txt').read()
>>> dictionary = file('data/dictionary.txt').read().split()
>>>
>>> g = Generator(sample, dictionary)
>>> g.generate_sentence() #doctest: +ELLIPSIS
(...)
>>>

Mais conteúdo relacionado

Mais procurados

Arm device tree and linux device drivers
Arm device tree and linux device driversArm device tree and linux device drivers
Arm device tree and linux device driversHoucheng Lin
 
Address/Thread/Memory Sanitizer
Address/Thread/Memory SanitizerAddress/Thread/Memory Sanitizer
Address/Thread/Memory SanitizerPlatonov Sergey
 
Building Network Functions with eBPF & BCC
Building Network Functions with eBPF & BCCBuilding Network Functions with eBPF & BCC
Building Network Functions with eBPF & BCCKernel TLV
 
How Linux Processes Your Network Packet - Elazar Leibovich
How Linux Processes Your Network Packet - Elazar LeibovichHow Linux Processes Your Network Packet - Elazar Leibovich
How Linux Processes Your Network Packet - Elazar LeibovichDevOpsDays Tel Aviv
 
Golang - Overview of Go (golang) Language
Golang - Overview of Go (golang) LanguageGolang - Overview of Go (golang) Language
Golang - Overview of Go (golang) LanguageAniruddha Chakrabarti
 
HKG15-311: OP-TEE for Beginners and Porting Review
HKG15-311: OP-TEE for Beginners and Porting ReviewHKG15-311: OP-TEE for Beginners and Porting Review
HKG15-311: OP-TEE for Beginners and Porting ReviewLinaro
 
Linux Ethernet device driver
Linux Ethernet device driverLinux Ethernet device driver
Linux Ethernet device driver艾鍗科技
 
20111015 勉強会 (PCIe / SR-IOV)
20111015 勉強会 (PCIe / SR-IOV)20111015 勉強会 (PCIe / SR-IOV)
20111015 勉強会 (PCIe / SR-IOV)Kentaro Ebisawa
 
以 eBPF 構建一個更為堅韌的 Kubernetes 叢集
以 eBPF 構建一個更為堅韌的 Kubernetes 叢集以 eBPF 構建一個更為堅韌的 Kubernetes 叢集
以 eBPF 構建一個更為堅韌的 Kubernetes 叢集HungWei Chiu
 
Encapsulation in C++
Encapsulation in C++Encapsulation in C++
Encapsulation in C++Hitesh Kumar
 

Mais procurados (20)

BusyBox for Embedded Linux
BusyBox for Embedded LinuxBusyBox for Embedded Linux
BusyBox for Embedded Linux
 
Arm device tree and linux device drivers
Arm device tree and linux device driversArm device tree and linux device drivers
Arm device tree and linux device drivers
 
How A Compiler Works: GNU Toolchain
How A Compiler Works: GNU ToolchainHow A Compiler Works: GNU Toolchain
How A Compiler Works: GNU Toolchain
 
Advanced C - Part 1
Advanced C - Part 1 Advanced C - Part 1
Advanced C - Part 1
 
Address/Thread/Memory Sanitizer
Address/Thread/Memory SanitizerAddress/Thread/Memory Sanitizer
Address/Thread/Memory Sanitizer
 
Building Network Functions with eBPF & BCC
Building Network Functions with eBPF & BCCBuilding Network Functions with eBPF & BCC
Building Network Functions with eBPF & BCC
 
Embedded C - Day 2
Embedded C - Day 2Embedded C - Day 2
Embedded C - Day 2
 
Qemu Pcie
Qemu PcieQemu Pcie
Qemu Pcie
 
How Linux Processes Your Network Packet - Elazar Leibovich
How Linux Processes Your Network Packet - Elazar LeibovichHow Linux Processes Your Network Packet - Elazar Leibovich
How Linux Processes Your Network Packet - Elazar Leibovich
 
FreeRTOS API
FreeRTOS APIFreeRTOS API
FreeRTOS API
 
Golang - Overview of Go (golang) Language
Golang - Overview of Go (golang) LanguageGolang - Overview of Go (golang) Language
Golang - Overview of Go (golang) Language
 
HKG15-311: OP-TEE for Beginners and Porting Review
HKG15-311: OP-TEE for Beginners and Porting ReviewHKG15-311: OP-TEE for Beginners and Porting Review
HKG15-311: OP-TEE for Beginners and Porting Review
 
Linux Ethernet device driver
Linux Ethernet device driverLinux Ethernet device driver
Linux Ethernet device driver
 
20111015 勉強会 (PCIe / SR-IOV)
20111015 勉強会 (PCIe / SR-IOV)20111015 勉強会 (PCIe / SR-IOV)
20111015 勉強会 (PCIe / SR-IOV)
 
以 eBPF 構建一個更為堅韌的 Kubernetes 叢集
以 eBPF 構建一個更為堅韌的 Kubernetes 叢集以 eBPF 構建一個更為堅韌的 Kubernetes 叢集
以 eBPF 構建一個更為堅韌的 Kubernetes 叢集
 
Encapsulation in C++
Encapsulation in C++Encapsulation in C++
Encapsulation in C++
 
Glibc malloc internal
Glibc malloc internalGlibc malloc internal
Glibc malloc internal
 
VerilatorとSystemC
VerilatorとSystemCVerilatorとSystemC
VerilatorとSystemC
 
C Programming - Refresher - Part III
C Programming - Refresher - Part IIIC Programming - Refresher - Part III
C Programming - Refresher - Part III
 
S emb t13-freertos
S emb t13-freertosS emb t13-freertos
S emb t13-freertos
 

Semelhante a Tdd with python unittest for embedded c

Test Driven iOS Development (TDD)
Test Driven iOS Development (TDD)Test Driven iOS Development (TDD)
Test Driven iOS Development (TDD)Babul Mirdha
 
Presentation slides: "How to get 100% code coverage"
Presentation slides: "How to get 100% code coverage" Presentation slides: "How to get 100% code coverage"
Presentation slides: "How to get 100% code coverage" Rapita Systems Ltd
 
Test Driven Development With Python
Test Driven Development With PythonTest Driven Development With Python
Test Driven Development With PythonSiddhi
 
Cross Platform App Development with C++
Cross Platform App Development with C++Cross Platform App Development with C++
Cross Platform App Development with C++Joan Puig Sanz
 
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docxeugeniadean34240
 
Static code analysis: what? how? why?
Static code analysis: what? how? why?Static code analysis: what? how? why?
Static code analysis: what? how? why?Andrey Karpov
 
TDD reloaded - JUGTAA 24 Ottobre 2012
TDD reloaded - JUGTAA 24 Ottobre 2012TDD reloaded - JUGTAA 24 Ottobre 2012
TDD reloaded - JUGTAA 24 Ottobre 2012Pietro Di Bello
 
Refactoring for testability c++
Refactoring for testability c++Refactoring for testability c++
Refactoring for testability c++Dimitrios Platis
 
Getting Started Cpp
Getting Started CppGetting Started Cpp
Getting Started CppLong Cao
 
Test Driven Development of A Static Code Analyzer
Test Driven Development of A Static Code AnalyzerTest Driven Development of A Static Code Analyzer
Test Driven Development of A Static Code AnalyzerTerry Yin
 
JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?Doug Hawkins
 
App secforum2014 andrivet-cplusplus11-metaprogramming_applied_to_software_obf...
App secforum2014 andrivet-cplusplus11-metaprogramming_applied_to_software_obf...App secforum2014 andrivet-cplusplus11-metaprogramming_applied_to_software_obf...
App secforum2014 andrivet-cplusplus11-metaprogramming_applied_to_software_obf...Cyber Security Alliance
 
Testing in Python: doctest and unittest
Testing in Python: doctest and unittestTesting in Python: doctest and unittest
Testing in Python: doctest and unittestFariz Darari
 
Testing in Python: doctest and unittest (Updated)
Testing in Python: doctest and unittest (Updated)Testing in Python: doctest and unittest (Updated)
Testing in Python: doctest and unittest (Updated)Fariz Darari
 

Semelhante a Tdd with python unittest for embedded c (20)

Test Driven iOS Development (TDD)
Test Driven iOS Development (TDD)Test Driven iOS Development (TDD)
Test Driven iOS Development (TDD)
 
Presentation slides: "How to get 100% code coverage"
Presentation slides: "How to get 100% code coverage" Presentation slides: "How to get 100% code coverage"
Presentation slides: "How to get 100% code coverage"
 
C Programming Tutorial - www.infomtec.com
C Programming Tutorial - www.infomtec.comC Programming Tutorial - www.infomtec.com
C Programming Tutorial - www.infomtec.com
 
Csdfsadf
CsdfsadfCsdfsadf
Csdfsadf
 
C
CC
C
 
C
CC
C
 
Test Driven Development With Python
Test Driven Development With PythonTest Driven Development With Python
Test Driven Development With Python
 
Cross Platform App Development with C++
Cross Platform App Development with C++Cross Platform App Development with C++
Cross Platform App Development with C++
 
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx
 
Static code analysis: what? how? why?
Static code analysis: what? how? why?Static code analysis: what? how? why?
Static code analysis: what? how? why?
 
TDD reloaded - JUGTAA 24 Ottobre 2012
TDD reloaded - JUGTAA 24 Ottobre 2012TDD reloaded - JUGTAA 24 Ottobre 2012
TDD reloaded - JUGTAA 24 Ottobre 2012
 
How to fake_properly
How to fake_properlyHow to fake_properly
How to fake_properly
 
Refactoring for testability c++
Refactoring for testability c++Refactoring for testability c++
Refactoring for testability c++
 
Getting Started Cpp
Getting Started CppGetting Started Cpp
Getting Started Cpp
 
Test Driven Development of A Static Code Analyzer
Test Driven Development of A Static Code AnalyzerTest Driven Development of A Static Code Analyzer
Test Driven Development of A Static Code Analyzer
 
JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?
 
Programming in C
Programming in CProgramming in C
Programming in C
 
App secforum2014 andrivet-cplusplus11-metaprogramming_applied_to_software_obf...
App secforum2014 andrivet-cplusplus11-metaprogramming_applied_to_software_obf...App secforum2014 andrivet-cplusplus11-metaprogramming_applied_to_software_obf...
App secforum2014 andrivet-cplusplus11-metaprogramming_applied_to_software_obf...
 
Testing in Python: doctest and unittest
Testing in Python: doctest and unittestTesting in Python: doctest and unittest
Testing in Python: doctest and unittest
 
Testing in Python: doctest and unittest (Updated)
Testing in Python: doctest and unittest (Updated)Testing in Python: doctest and unittest (Updated)
Testing in Python: doctest and unittest (Updated)
 

Mais de Benux Wei

F9 microkernel app development part 2 gpio meets led
F9 microkernel app development part 2 gpio meets ledF9 microkernel app development part 2 gpio meets led
F9 microkernel app development part 2 gpio meets ledBenux Wei
 
F9 microkernel app development part 1
F9 microkernel app development part 1F9 microkernel app development part 1
F9 microkernel app development part 1Benux Wei
 
F9 microkernel code reading part 4 memory management
F9 microkernel code reading part 4 memory managementF9 microkernel code reading part 4 memory management
F9 microkernel code reading part 4 memory managementBenux Wei
 
F9 Microkernel code reading part 2 scheduling
F9 Microkernel code reading part 2 schedulingF9 Microkernel code reading part 2 scheduling
F9 Microkernel code reading part 2 schedulingBenux Wei
 
While software engineer meets 3d printer
While software engineer meets 3d printerWhile software engineer meets 3d printer
While software engineer meets 3d printerBenux Wei
 
F9 Microkernel code reading - part 1
F9 Microkernel code reading - part 1F9 Microkernel code reading - part 1
F9 Microkernel code reading - part 1Benux Wei
 
Real practice of Networking design on specialized for ARM Cortex-M
Real practice of Networking design on specialized for ARM Cortex-MReal practice of Networking design on specialized for ARM Cortex-M
Real practice of Networking design on specialized for ARM Cortex-MBenux Wei
 
Stm32 f4 first touch
Stm32 f4 first touchStm32 f4 first touch
Stm32 f4 first touchBenux Wei
 
Preparation for mit ose lab4
Preparation for mit ose lab4Preparation for mit ose lab4
Preparation for mit ose lab4Benux Wei
 

Mais de Benux Wei (9)

F9 microkernel app development part 2 gpio meets led
F9 microkernel app development part 2 gpio meets ledF9 microkernel app development part 2 gpio meets led
F9 microkernel app development part 2 gpio meets led
 
F9 microkernel app development part 1
F9 microkernel app development part 1F9 microkernel app development part 1
F9 microkernel app development part 1
 
F9 microkernel code reading part 4 memory management
F9 microkernel code reading part 4 memory managementF9 microkernel code reading part 4 memory management
F9 microkernel code reading part 4 memory management
 
F9 Microkernel code reading part 2 scheduling
F9 Microkernel code reading part 2 schedulingF9 Microkernel code reading part 2 scheduling
F9 Microkernel code reading part 2 scheduling
 
While software engineer meets 3d printer
While software engineer meets 3d printerWhile software engineer meets 3d printer
While software engineer meets 3d printer
 
F9 Microkernel code reading - part 1
F9 Microkernel code reading - part 1F9 Microkernel code reading - part 1
F9 Microkernel code reading - part 1
 
Real practice of Networking design on specialized for ARM Cortex-M
Real practice of Networking design on specialized for ARM Cortex-MReal practice of Networking design on specialized for ARM Cortex-M
Real practice of Networking design on specialized for ARM Cortex-M
 
Stm32 f4 first touch
Stm32 f4 first touchStm32 f4 first touch
Stm32 f4 first touch
 
Preparation for mit ose lab4
Preparation for mit ose lab4Preparation for mit ose lab4
Preparation for mit ose lab4
 

Último

Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 

Último (20)

Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 

Tdd with python unittest for embedded c

  • 1. TDD with Python unittest for embedded C Ben6 2012-08-28 www.juluos.org
  • 2. Agenda ● TDD是什麼? ● 為什麼使用TDD? ● python unittest ○ C & Embedded C ● Real Practice 1. BOS 實際導入 2. Dummy test and LED Driver
  • 4. What's TDD? 測試先行 意思就是,在開發程式時, 要以測試的角度來設計。
  • 5. Red 1. Write a test that fails 先寫一個測試結果錯誤 TDD
  • 6. Red 1. Write a test that fails 先寫一個測試結果錯誤 TDD 2. Make the code work Green 使程式可用
  • 7. Red 1. Write a test that fails 先寫一個測試結果錯誤 3. Eliminate redundancy TDD 消除冗餘 2. Make the code work Refactoring Green 使程式可用
  • 8. Red 1. Write a test that fails 先寫一個測試結果錯誤 3. Eliminate redundancy TDD 消除冗餘 2. Make the code work Refactoring Green 使程式可用 TDD 的開發節奏 "Red, Green, Refactoring"
  • 9. TDD的觀點 All code is guilty until proven innocent. 任何代碼都是有問題的,直到證明他無誤。
  • 14. Python unittest $ python sample.py setUp() testCase1() tearDown() .setUp() testCase2() tearDown() . ----------------------------------------- ----------------------------- Ran 2 tests in 0.000s OK
  • 15. Python unittest import random import unittest class TestSequenceFunctions(unittest.TestCase): def setUp(self): self.seq = range(10) def test_shuffle(self): random.shuffle(self.seq) self.seq.sort() self.assertEqual(self.seq, range(10)) self.assertRaises(TypeError, random.shuffle, (1,2,3)) def test_choice(self): element = random.choice(self.seq) self.assertTrue(element in self.seq) def test_sample(self): with self.assertRaises(ValueError): random.sample(self.seq, 20) for element in random.sample(self.seq, 5): self.assertTrue(element in self.seq) if __name__ == '__main__': unittest.main()
  • 16. Python 與 C c library: libfoo.so int foo(unsigned int r); python ctypes sample from ctypes import * foolib = CDLL.LoadLibrary('libfoo.so') r = foolib.foo(c_uint(5))
  • 17. Embedded C Definitions ● Related to hardware environment ● Ex: A driver ■ LED ■ Netowrk Interface Card ■ VGA Card ■ self test ■ ...
  • 18. ctypes ctypes type C type Python type c_bool _Bool bool (1) c_char char 1-character string c_wchar wchar_t 1-character unicode string c_byte char int/long c_ubyte unsigned char int/long c_short short int/long c_ushort unsigned short int/long c_int int int/long c_uint unsigned int int/long c_long long int/long c_ulong unsigned long int/long c_longlong __int64 or long long int/long c_ulonglong unsigned __int64 or unsigned long long int/long c_float float float c_double double float c_longdouble long double float c_char_p char * (NUL terminated) string or None c_wchar_p wchar_t * (NUL terminated) unicode or None c_void_p void * int/long or None
  • 19. sample ctypes >>> from ctypes import * >>> p = create_string_buffer(3) >>> print sizeof(p), repr(p.raw) 3 'x00x00x00' >>> p = create_string_buffer("Hello") >>> print sizeof(p), repr(p.raw) 6 'Hellox00' >>> print repr(p.value) 'Hello'
  • 20. sample ctypes >>> from ctypes import * >>> p = create_string_buffer("Hello", 10) >>> print sizeof(p), repr(p.raw) 10 'Hellox00x00x00x00x00' >>> p.value = "Hi" >>> print sizeof(p), repr(p.raw) 10 'Hix00lox00x00x00x00x00'
  • 21. python cdll in different platform from ctypes import * import sys platform = sys.platform if sys.platform == "cygwin": libc = cdll.LoadLibrary("/bin/cygwin1.dll") else: libc = CDLL('libc.so.6')
  • 22. python cdll in different platform (cont.) import sys from math import log def is64bit(): return log(sys.maxsize, 2) == 63 arch=32 if is64bit(): arch = 64
  • 23. 案例研討(一) BOS 實際導入 blibc pytest @ github
  • 24. CFILES= ../../blibc/itoa.c OBJS=$(CFILES:.c=.o) CFLAGS= -I ../../include Makefile for BLIBC_SHARED = libbosc.so unittest blibc all: $(BLIBC_SHARED) python alltests.py %.o :%.c $(CC) -c $< -o $@ $(CFLAGS) $(BLIBC_SHARED): $(OBJS) $(CC) -shared -o $@ $^ clean: rm -f $(BLIBC_SHARED) $(OBJS)
  • 25. import unittest,os from ctypes import * app_path = os.path.dirname(os.path.abspath(__file__)) libpathname = os.path.join(app_path, "./libbosc.so") bc = CDLL(libpathname); class BOSTest_atoi(unittest.TestCase): s = (c_byte*9)() c = 427 def test_atoi(self): alltests.py # ... next page def suite_blibc(): bosTestSuite = unittest.makeSuite(BOSTest_atoi, 'test') return bosTestSuite def main(): suite1 = suite_blibc() alltests = unittest.TestSuite((suite1)) runner = unittest.TextTestRunner() runner.run(alltests);
  • 26. def test_atoi(self): test 1 in alltests.py # const char *itohex(uint32_t c, char *s, int size, int upper) b = bc.itohex(self.c, self.s, c_int(9), c_int(0)) hex_str = string_at(self.s) assert hex_str == "000001ab", "atoi padding hex string" assert string_at(b) == "1ab"," atoi incorrect no-zero hex padding" test 2 in alltests.py def test_auto_with_upper(self): upper_hex = bc.itohex(self.c, self.s, c_int(9), c_int(1)) assert string_at(upper_hex) == "1AB", " atoi incorrect no-zero upper hex padding"
  • 28. 實戰演練(一)發生錯誤的單元測試 空的測試腳本包含: ● a dummy python test (alltests.py) ○ use python ctypes to load lib function ● a sample Makefile to create shared library for python unitest git clone -b tdd_leds_sample git@github.com:benwei/JuluOS.git tdd_leds $ cd tdd_leds/tests $ git checkout -b your_tdd1 a325e85366da5d0d3735863aa983a27700829a28 $ make python alltests.py E ====================================================================== ERROR: test_turn_onoff (__main__.BOSTest_led) ---------------------------------------------------------------------- Traceback (most recent call last): File "alltests.py", line 21, in test_turn_onoff led.turn_on(c_char(0)) TypeError: one character string expected ---------------------------------------------------------------------- Ran 1 test in 0.000s FAILED (errors=1)
  • 29. 實戰演練(一)使程式通過測試 1. 建立 ../drivers/led.c 2. 並使之通過測試 結果如下: $ make cc -c ../drivers/led.c -o ../drivers/led.o -I ../inc cc -shared -o libled.so ../drivers/led.o python alltests.py after turnon 1 after turnoff 0 . ---------------------------------------------------------------------- Ran 1 test in 0.001s OK
  • 30. 實戰演練(二) Refactoring 及 TDD使用Mock LED(s) Driver
  • 31. 實際演練(二)LED Driver with TDD Make a test that fails ● Create dummy test function ● Create a Mock LED Device for monitoring ● Add LED turn_on/off api to test function ● Check expect result of LED state Make code work ● write the operation code for LED Refactoring ● refined wording, duplicated code, naming ...
  • 32. Mock LED(s) Device ● 可是一個記憶體位置 ● 可為表示狀態的指示器 ● 一個簡化後的虛擬裝置 Ex: uint mock_led = 0; led_plug(&mock_led, id_1); Mock Object 在開發過程十分重要,因為當測試環境愈單純,將會更易於自 ! 動化測試的建構。http://en.wikipedia.org/wiki/Mock_object
  • 33. 實際演練(二) ● 練習題 ○ 擴充為十個LED 燈,可以同時點亮,也可單獨開關其中 之一。 完成後原始碼: $ git checkout tdd_leds_sample $ git checkout 453b80514da4793f6e608343742ba89bb870dcd6
  • 34. 實際演練(二)TDD完成輸入畫面 git clone -b tdd_leds_sample git@github.com:benwei/JuluOS.git tdd_leds $ cd tdd_leds/tests tests$ ls alltests.py Makefile tests$ make cc -c ../drivers/leds.c -o ../drivers/leds.o -I ../inc cc -shared -o libleds.so ../drivers/leds.o python alltests.py after turnon 1 .. ---------------------------------------------------------------------- Ran 2 tests in 0.000s OK
  • 35. 實際演練(二)延伸練習 ● 使用者可由外部傳入LED Mock Objects ● 使用者可使用不同顏色LED ● 使用者可設任一LED燈亮1~10秒 ○ 使用python + timer check 來確定其執行時間
  • 37. TDD 好處 較少的bug 減少手動重複測試的時間 文件檔案 容易改善設計,因不將設計的耦合降低,將無法進行單元測試 根據測式程式後的記錄,能很清楚知道目前的進度。 很關卡遊戲,設置問題,填入解答(可不斷精鍊) ,獲得完成時的成就感。
  • 38. 1.Write a test that fails TDD Eliminate 2. Make redundancy the code work The mantra of TDD is "Red, green, refactoring"
  • 39. Ending - Test-driven development a programming technique that requires you to write actual code automated test code simultaneously ensures you test your code enables you to retest your code quickly and easily, since it’s automated
  • 40. Q&A? Maintain write a test fails TDD? Refactor duplicate
  • 42. References Mock Objects for TDD ● Why and When to Use Mock Objects ● library unittest mock ● wikipedia.org/wiki/Mock_object
  • 43. How python with cli? cli - interface ? ● input via arguments ● cli output ○ update the specific files ○ exit code ○ console output ● use unittest program testing cli output
  • 44. ctypes use global variable in lib led = CDLL("./libled.so") v = c_uint.in_dll(led, "_led") print v.value() Reference: http://docs.python.org/library/ctypes. html#accessing-values-exported-from-dlls ! 筆者的意見:儘量不要直接存取全域變數;經由封裝函數介面來使用, 如:uint get_led(void) { return _led; }
  • 45. loremipsum A Lorem Ipsum text generator pypi.python.org/pypi/loremipsum/1.0.2 >>> from loremipsum import Generator >>> >>> sample = file('data/sample.txt').read() >>> dictionary = file('data/dictionary.txt').read().split() >>> >>> g = Generator(sample, dictionary) >>> g.generate_sentence() #doctest: +ELLIPSIS (...) >>>