51Testing
怬
µçÄÔ°æ

Ò»¸ö¼òµ¥ºÃÓõÄCÓïÑÔµ¥Ôª²âÊÔ¿ò¼Ü-Unity

2024-4-09 09:31×÷ÕߣºØýÃûÀ´Ô´£º51CTO²©¿Í

¡¡¡¡Unity¼ò½é£º
¡¡¡¡UnityÊÇÒ»¸öÓÃÓÚCÓïÑÔµÄÇáÁ¿¼¶µ¥Ôª²âÊÔ¿ò¼Ü¡£ËüÓÉThrow The SwitchÍŶӿª·¢£¬Ö¼ÔÚ¼ò»¯Ç¶ÈëʽϵͳµÄµ¥Ôª²âÊÔ¡£µ¥Ôª²âÊÔÖе¥ÔªµÄº¬Ò壬µ¥Ôª¾ÍÊÇÈËΪ¹æ¶¨µÄ×îСµÄ±»²â¹¦ÄÜÄ£¿é£¬ÈçCÓïÑÔÖе¥ÔªÖ¸Ò»¸öº¯Êý£¬JavaÀïµ¥ÔªÖ¸Ò»¸öÀ࣬ͼÐλ¯µÄÈí¼þÖпÉÒÔÖ¸Ò»¸ö´°¿Ú»òÒ»¸ö²Ëµ¥µÈ¡£ÔÚʵ¼ÊÏîÄ¿ÖУ¬µ¥Ôª²âÊÔÍùÍùÓÉ¿ª·¢ÈËÔ±Íê³É¡£
¡¡¡¡UnityµÄÉè¼ÆÄ¿±êÊÇÒ×ÓÚʹÓá¢Çá±ã¡¢¿ÉÒÆÖ²£¬²¢Äܹ»ÔÚ¸÷ÖÖǶÈëʽºÍ·ÇǶÈëʽϵͳÖÐÔËÐС£ºËÐÄÏîÄ¿ÊÇÒ»¸ö C ÎļþºÍÒ»¶ÔÍ·Îļþ£¬ÔÊÐí½«ÆäÌí¼Óµ½ÏÖÓеĹ¹½¨ÉèÖÃÖУ¬¶ø²»»áÌ«Âé·³¡£ ¿ÉÒÔʹÓÃÈκÎÏëÓõıàÒëÆ÷£¬²¢ÇÒ¿ÉÒÔʹÓôó¶àÊýÏÖÓеĹ¹½¨ÏµÍ³£¬°üÀ¨ Make¡¢CMake µÈ¡£
¡¡¡¡Unity¼òµ¥Ê¹Ó÷½·¨£º
¡¡¡¡1¡¢ÏÂÔØunity¿ò¼Ü
¡¡¡¡È¥GitHubÏÂÔØ£¬»òÕßgti cloneµ½±¾µØ£¬Á´½ÓµØÖ·£ºunity¡£
¡¡¡¡Unity ±¾Éí·Ç³£Ð¡¡£ ÆäËûʣϵÄÖ»ÊÇ¿ÉÑ¡¸½¼Ó×é¼þ¡£ ¿ÉÒÔºöÂÔËü»òÔÚ·½±ãʱʹÓÃËü¡£ ÒÔÏÂÊÇÏîÄ¿ÖÐËùÓÐÄÚÈݵĸÅÊö¡£
¡¡¡¡¡¤src- ÕâÊÇÄã¹ØÐĵĴúÂ룡´ËÎļþ¼Ð°üº¬Ò»¸ö C ÎļþºÍÁ½¸öÍ·Îļþ¡£ ÕâÈý¸öÎļþÊÇ Unity¡£
¡¡¡¡¡¤ docs- ÔÚÕâÀï¿ÉÒÔÕÒµ½ËùÓз½±ãµÄÎĵµ¡£
¡¡¡¡¡¤ examples- Õâ°üº¬Ò»Ð©Ê¹Óà Unity µÄʾÀý¡£
¡¡¡¡¡¤ extras- ÕâЩÊÇ Unity µÄ¿ÉÑ¡¸½¼Ó×é¼þ£¬²»ÊôÓÚºËÐÄÏîÄ¿¡£
¡¡¡¡¡¤ test- Õâ¾ÍÊÇ Unity ¼°Æä½Å±¾µÄ²âÊÔ·½Ê½¡£ Èç¹ûÄãÖ»ÊÇʹÓÃUnity£¬Äã¿ÉÄÜÓÀÔ¶²»ÐèÒª½øÈëÕâÀï¡£
¡¡¡¡¡¤ auto- ÔÚÕâÀÄ㽫ÕÒµ½ÓÐÓÃµÄ Ruby ½Å±¾£¬ÒÔ¼ò»¯ÄãµÄ²âÊÔ¹¤×÷Á÷³Ì¡£ ËüÃÇÍêÈ«ÊÇ¿ÉÑ¡µÄ£¬²»ÐèҪʹÓà Unity¡£
¡¡¡¡2¡¢Ê¹ÓÃunity¿ò¼Ü
¡¡¡¡²âÊÔÎļþÊÇ C Îļþ¡£ ´ó¶àÊýÇé¿öÏ£¬½«ÎªÃ¿¸öÒª²âÊ﵀ C Ä£¿é´´½¨Ò»¸ö²âÊÔÎļþ¡£ ²âÊÔÎļþÓ¦°üº¬ unity.h ºÍÒª²âÊ﵀ C Ä£¿éµÄ±êÍ·¡£
¡¡¡¡ÀýÈç²âÊÔÏÂÃæÁ½¸öº¯Êý£º
¡¡¡¡ProductionCode.c
¡¡¡¡#include "ProductionCode.h"
¡¡¡¡int Counter = 0;
¡¡¡¡int NumbersToFind[9] = { 0, 34, 55, 66, 32, 11, 1, 77, 888 }; /* some obnoxious array to search that is 1-based indexing instead of 0. */
¡¡¡¡/* This function is supposed to search through NumbersToFind and find a particular number.
¡¡¡¡ * If it finds it, the index is returned.  Otherwise 0 is returned which sorta makes sense since
¡¡¡¡ * NumbersToFind is indexed from 1.  Unfortunately it's broken
¡¡¡¡ * (and should therefore be caught by our tests) */
¡¡¡¡int FindFunction_WhichIsBroken(int NumberToFind)
¡¡¡¡{
¡¡¡¡    int i = 0;
¡¡¡¡    while (i < 8) /* Notice I should have been in braces */
¡¡¡¡        i++;
¡¡¡¡        if (NumbersToFind[i] == NumberToFind) /* Yikes!  I'm getting run after the loop finishes instead of during it! */
¡¡¡¡            return i;
¡¡¡¡    return 0;
¡¡¡¡}
¡¡¡¡int FunctionWhichReturnsLocalVariable(void)
¡¡¡¡{
¡¡¡¡    return Counter;
¡¡¡¡}
¡¡¡¡ProductionCode.h
¡¡¡¡#ifndef  _PRODICTIONCODE_H_
¡¡¡¡#define  _PRODICTIONCODE_H_
¡¡¡¡extern int Counter;
¡¡¡¡int FindFunction_WhichIsBroken(int NumberToFind);
¡¡¡¡int FunctionWhichReturnsLocalVariable(void);
¡¡¡¡#endif
¡¡¡¡½ÓÏÂÀ´£¬²âÊÔÎļþ½«°üº¬²âÊÔÀý³Ìº¯Êý¡£ setUp º¯Êý¿ÉÒÔ°üº¬ÄãÏ£ÍûÔÚÿ´Î²âÊÔ֮ǰÔËÐеÄÈκÎÄÚÈÝ¡£ tearDown º¯Êý¿ÉÒÔ°üº¬ÄãÏ£ÍûÔÚÿ´Î²âÊÔºóÔËÐеÄÈκÎÄÚÈÝ¡£ ÕâÁ½¸öº¯Êý¶¼²»½ÓÊÜÈκβÎÊý£¬Ò²²»·µ»ØÈκÎÄÚÈÝ¡£ Èç¹û²»ÐèÒªËüÃÇ£¬½«ÆäÉèÖÃΪ¿Õ¡£setUp() tearDown()
¡¡¡¡TestProductionCode.h
¡¡¡¡#include "ProductionCode.h"
¡¡¡¡#include "unity.h"
¡¡¡¡/* sometimes you may want to get at local data in a module.
¡¡¡¡ * for example: If you plan to pass by reference, this could be useful
¡¡¡¡ * however, it should often be avoided */
¡¡¡¡extern int Counter;
¡¡¡¡void setUp(void)
¡¡¡¡{
¡¡¡¡  /* This is run before EACH TEST */
¡¡¡¡  Counter = 0x5a5a;
¡¡¡¡}
¡¡¡¡void tearDown(void)
¡¡¡¡{
¡¡¡¡}
¡¡¡¡void test_FindFunction_WhichIsBroken_ShouldReturnZeroIfItemIsNotInList_WhichWorksEvenInOurBrokenCode(void)
¡¡¡¡{
¡¡¡¡  /* All of these should pass */
¡¡¡¡  TEST_ASSERT_EQUAL(0, FindFunction_WhichIsBroken(78));
¡¡¡¡  TEST_ASSERT_EQUAL(0, FindFunction_WhichIsBroken(2));
¡¡¡¡  TEST_ASSERT_EQUAL(0, FindFunction_WhichIsBroken(33));
¡¡¡¡  TEST_ASSERT_EQUAL(0, FindFunction_WhichIsBroken(999));
¡¡¡¡  TEST_ASSERT_EQUAL(0, FindFunction_WhichIsBroken(-1));
¡¡¡¡}
¡¡¡¡void test_FindFunction_WhichIsBroken_ShouldReturnTheIndexForItemsInList_WhichWillFailBecauseOurFunctionUnderTestIsBroken(void)
¡¡¡¡{
¡¡¡¡  /* You should see this line fail in your test summary */
¡¡¡¡  TEST_ASSERT_EQUAL(1, FindFunction_WhichIsBroken(34));
¡¡¡¡  /* Notice the rest of these didn't get a chance to run because the line above failed.
¡¡¡¡   * Unit tests abort each test function on the first sign of trouble.
¡¡¡¡   * Then NEXT test function runs as normal. */
¡¡¡¡  TEST_ASSERT_EQUAL(8, FindFunction_WhichIsBroken(8888));
¡¡¡¡}
¡¡¡¡void test_FunctionWhichReturnsLocalVariable_ShouldReturnTheCurrentCounterValue(void)
¡¡¡¡{
¡¡¡¡    /* This should be true because setUp set this up for us before this test */
¡¡¡¡    TEST_ASSERT_EQUAL_HEX(0x5a5a, FunctionWhichReturnsLocalVariable());
¡¡¡¡    /* This should be true because we can still change our answer */
¡¡¡¡    Counter = 0x1234;
¡¡¡¡    TEST_ASSERT_EQUAL_HEX(0x1234, FunctionWhichReturnsLocalVariable());
¡¡¡¡}
¡¡¡¡void test_FunctionWhichReturnsLocalVariable_ShouldReturnTheCurrentCounterValueAgain(void)
¡¡¡¡{
¡¡¡¡    /* This should be true again because setup was rerun before this test (and after we changed it to 0x1234) */
¡¡¡¡    TEST_ASSERT_EQUAL_HEX(0x5a5a, FunctionWhichReturnsLocalVariable());
¡¡¡¡}
¡¡¡¡void test_FunctionWhichReturnsLocalVariable_ShouldReturnCurrentCounter_ButFailsBecauseThisTestIsActuallyFlawed(void)
¡¡¡¡{
¡¡¡¡    /* Sometimes you get the test wrong.  When that happens, you get a failure too... and a quick look should tell
¡¡¡¡     * you what actually happened...which in this case was a failure to setup the initial condition. */
¡¡¡¡    TEST_ASSERT_EQUAL_HEX(0x1234, FunctionWhichReturnsLocalVariable());
¡¡¡¡}
¡¡¡¡×îºó£¬ÔÚ±àдmainº¯Êý£¬È»ºóµ÷ÓÃÿ¸ö²âÊÔÀý³Ìº¯Êý¡£ Õâʵ¼ÊÉϻᴥ·¢Ã¿¸ö²âÊÔÀý³Ìº¯ÊýÔËÐУ¬Òò´Ëÿ¸öº¯Êý¶¼ÓÐ×Ô¼ºµÄµ÷Ó÷dz£ÖØÒª¡£
¡¡¡¡/* AUTOGENERATED FILE. DO NOT EDIT. */
¡¡¡¡/*=======Automagically Detected Files To Include=====*/
¡¡¡¡#include "unity.h"
¡¡¡¡#include <setjmp.h>
¡¡¡¡#include <stdio.h>
¡¡¡¡#include "ProductionCode.h"
¡¡¡¡/*=======External Functions This Runner Calls=====*/
¡¡¡¡extern void setUp(void);
¡¡¡¡extern void tearDown(void);
¡¡¡¡extern void test_FindFunction_WhichIsBroken_ShouldReturnZeroIfItemIsNotInList_WhichWorksEvenInOurBrokenCode(void);
¡¡¡¡extern void test_FindFunction_WhichIsBroken_ShouldReturnTheIndexForItemsInList_WhichWillFailBecauseOurFunctionUnderTestIsBroken(void);
¡¡¡¡extern void test_FunctionWhichReturnsLocalVariable_ShouldReturnTheCurrentCounterValue(void);
¡¡¡¡extern void test_FunctionWhichReturnsLocalVariable_ShouldReturnTheCurrentCounterValueAgain(void);
¡¡¡¡extern void test_FunctionWhichReturnsLocalVariable_ShouldReturnCurrentCounter_ButFailsBecauseThisTestIsActuallyFlawed(void);
¡¡¡¡/*=======MAIN=====*/
¡¡¡¡int main(void)
¡¡¡¡{
¡¡¡¡  UnityBegin("test/TestProductionCode.c");
¡¡¡¡  RUN_TEST(test_FindFunction_WhichIsBroken_ShouldReturnZeroIfItemIsNotInList_WhichWorksEvenInOurBrokenCode);
¡¡¡¡  RUN_TEST(test_FindFunction_WhichIsBroken_ShouldReturnTheIndexForItemsInList_WhichWillFailBecauseOurFunctionUnderTestIsBroken);
¡¡¡¡  RUN_TEST(test_FunctionWhichReturnsLocalVariable_ShouldReturnTheCurrentCounterValue);
¡¡¡¡  RUN_TEST(test_FunctionWhichReturnsLocalVariable_ShouldReturnTheCurrentCounterValueAgain);
¡¡¡¡  RUN_TEST(test_FunctionWhichReturnsLocalVariable_ShouldReturnCurrentCounter_ButFailsBecauseThisTestIsActuallyFlawed);
¡¡¡¡  return (UnityEnd());
¡¡¡¡}
¡¡¡¡3¡¢¹¹½¨ÔËÐл·¾³
¡¡¡¡´´½¨Ò»¸öĿ¼ TDDUnityExample£¬ÔÚTDDUnityExample Ŀ¼Ï´´½¨ CMakeLists.txt Îļþ¡£
¡¡¡¡# ×îµÍCMake°æ±¾ÒªÇó
¡¡¡¡cmake_minimum_required(VERSION 3.15)
¡¡¡¡#½«Unity/src¹¤×÷Ŀ¼µÄÔ´Îļþ¸³¸øUNITY_SRC_LIST
¡¡¡¡#½«Tests¹¤×÷Ŀ¼µÄÔ´Îļþ¸³¸øAPP_SRC_DIR
¡¡¡¡aux_source_directory (Unity/src UNITY_SRC_LIST)
¡¡¡¡aux_source_directory (Tests APP_SRC_DIR)
¡¡¡¡# ÏîÄ¿Ãû³Æ
¡¡¡¡project(TDD_test)
¡¡¡¡# Í·Îļþ·¾¶
¡¡¡¡include_directories(Unity/src)
¡¡¡¡#½«ËùÓÐÔ´ÎļþÉú³ÉÒ»¸ö¿ÉÖ´ÐÐÎļþ
¡¡¡¡add_executable(TDD_test  ${APP_SRC_DIR} ${UNITY_SRC_LIST})
¡¡¡¡set (EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
¡¡¡¡ÔÚ TDDUnityExample Ï´´½¨ Unity Ŀ¼£¬È»ºó½«Unity ¿ò¼ÜÔ´ÂëÖÐµÄ src Ŀ¼¿½±´µ½ UnityĿ¼ÖС£
¡¡¡¡ÔÚ TDDUnityExample Ï´´½¨ Tests Ŀ¼£¬È»ºó½«µÚ¶þµãÖоÙÀýµÄ²âÊÔÎļþ¶¼·ÅÔÚ Tests Ŀ¼ÖС£
¡¡¡¡ÔÚ TDDUnityExample Ï´´½¨ build Ŀ¼£¬È»ºóÔÚ build Ŀ¼ÏÂÒ²´´½¨Ò»¸ö CMakeLists.txt Îļþ¡£
¡¡¡¡cmake_minimum_required (VERSION 2.8)
¡¡¡¡project (TDD_test)
¡¡¡¡add_subdirectory (Tests)
¡¡¡¡×îºóÕûÌåÎļþ½á¹¹ÈçÏ£º
¡¡¡¡©À©¤©¤ build
¡¡¡¡©¦   ©¸©¤©¤ CMakeLists.txt
¡¡¡¡©À©¤©¤ CMakeLists.txt
¡¡¡¡©À©¤©¤ Tests
¡¡¡¡©¦   ©À©¤©¤ ProductionCode.c
¡¡¡¡©¦   ©À©¤©¤ ProductionCode.h
¡¡¡¡©¦   ©À©¤©¤ TestProductionCode.c
¡¡¡¡©¦   ©¸©¤©¤ TestProductionCode_Runner.c
¡¡¡¡©¸©¤©¤ Unity
¡¡¡¡    ©¸©¤©¤ src
¡¡¡¡        ©À©¤©¤ meson.build
¡¡¡¡        ©À©¤©¤ unity.c
¡¡¡¡        ©À©¤©¤ unity.h
¡¡¡¡        ©¸©¤©¤ unity_internals.h
¡¡¡¡4¡¢ÔËÐвâÊÔ³ÌÐò
¡¡¡¡ÔÚ build Ŀ¼ÏÂÊäÈë cmake ..ÃüÁ»á×Ô¶¯Éú³É Makefile Îļþ£¬È»ºóÊäÈëmake£¬¾Í»á×Ô¶¯±àÒëÉú³É¿ÉÖ´ÐÐÎļþ TDD_test ÔÚ TDDUnityExample /bin Ï¡£
¡¡¡¡½øÈë TDDUnityExample /bin Ï£¬ÊäÈë./TDD_test¡£
¡¡¡¡Êä³ö½á¹ûΪ£º
¡¡¡¡test/TestProductionCode.c:0:test_FindFunction_WhichIsBroken_ShouldReturnZeroIfItemIsNotInList_WhichWorksEvenInOurBrokenCode:PASS
¡¡¡¡test/TestProductionCode.c:33:test_FindFunction_WhichIsBroken_ShouldReturnTheIndexForItemsInList_WhichWillFailBecauseOurFunctionUnderTestIsBroken:FAIL: Expected 1 Was 0
¡¡¡¡test/TestProductionCode.c:0:test_FunctionWhichReturnsLocalVariable_ShouldReturnTheCurrentCounterValue:PASS
¡¡¡¡test/TestProductionCode.c:0:test_FunctionWhichReturnsLocalVariable_ShouldReturnTheCurrentCounterValueAgain:PASS
¡¡¡¡test/TestProductionCode.c:61:test_FunctionWhichReturnsLocalVariable_ShouldReturnCurrentCounter_ButFailsBecauseThisTestIsActuallyFlawed:FAIL: Expected 0x00001234 Was 0x00005A5A
¡¡¡¡-----------------------
¡¡¡¡5 Tests 2 Failures 0 Ignored 
¡¡¡¡FAIL
¡¡¡¡´Ó½á¹ûÀ´¿´µÚ¶þ¸öºÍµÚÎå¸ö²âÊÔÀý³Ì³öÏÖ´íÎ󣬴íÎóÔ­ÒòÒ²±ê¼Ç³öÀ´ÁË¡£
¡¡¡¡5¡¢³£ÓöÏÑÔ
¡¡¡¡TEST_PASS(); /* ÖÐÖ¹²âÊÔµÄÆäÓಿ·Ö, µ«½«²âÊÔ¼ÆΪͨ¹ý */
¡¡¡¡TEST_PASS_MESSAGE("message")
¡¡¡¡TEST_IGNORE(); /* ½«²âÊÔÓÃÀý±ê¼ÇΪºöÂÔ, µ«½«²âÊÔ¼ÆΪͨ¹ý */
¡¡¡¡TEST_PASS_MESSAGE("message")
¡¡¡¡    
¡¡¡¡TEST_FAIL(); /* ÖÐÖ¹²âÊÔµÄÆäÓಿ·Ö, µ«½«²âÊÔ¼ÆΪʧ°Ü */
¡¡¡¡TEST_FAIL_MESSAGE("message")
¡¡¡¡    
¡¡¡¡TEST_MESSAGE(""); /* ½«ÏûÏ¢Êä³ö */
¡¡¡¡ÒÔÉ϶ÏÑԺ궼ÊÇ·ÅÔÚ±àдµÄ²âÊÔº¯ÊýÖУ¬TEST_IGNORE Ò»°ã·ÅÔÚº¯ÊýµÄ¶¥²¿£¬ÓÃÀ´±íʾ½«²âÊÔÓÃÀýºöÂÔ£¬ÆäËûºê¿ÉÒÔ·ÅÔÚº¯ÊýµÄÈÎÒâλÖá£
¡¡¡¡×ܽ᣺
¡¡¡¡unityµ¥Ôª²âÊÔ¿ò¼ÜºËÐÄÊÇÒ»¸ö C ÎļþºÍÒ»¶ÔÍ·Îļþ£¬ÌصãÊǼò½àÒ×Óá¢ÇáÁ¿¼¶¡¢¿ÉÒÆÖ²ÐÔ¡¢Ö§³Ö²âÊÔ¶ÏÑԵȡ£ÆäÖÐÓÐÐí¶à²âÊÔ¶ÏÑÔÐèÒª¶àÁ˽â¶àʹÓá£
¡¡¡¡ºÃÁËÒÔÉϾÍÊÇUnityµ¥Ôª²âÊÔ¿ò¼ÜµÄ¼òÒ×ʹÓ÷½·¨£¬ÓÐʲôÒÉÎʺͽ¨Ò黶ӭÔÚÆÀÂÛÇøÖÐÌá³ö£¬ÏëÒªÁ˽â¸ü¶àµÄUnity֪ʶ¿ÉÒÔÈ¥¹ÙÍøÉϲ鿴£¬¹ÙÍøÉÏÒ²ÓÐÏêϸµÄ½Ì³ÌºÍʵÀý¡£
¡¡¡¡±¾ÎÄÄÚÈݲ»ÓÃÓÚÉÌҵĿµÄ£¬ÈçÉ漰֪ʶ²úȨÎÊÌ⣬ÇëȨÀûÈËÁªÏµ51TestingС±à(021-64471599-8017)£¬ÎÒÃǽ«Á¢¼´´¦Àí
Èí¼þ²âÊÔ µ¥Ôª²âÊÔ
µ±Ç°Ã»ÓÐÆÀÂÛµã»÷·¢±íÆÀÂÛ

Ïà¹ØÔĶÁ