mirror of
https://github.com/ThrowTheSwitch/Unity.git
synced 2026-10-26 18:23:33 +00:00
Added another example
* Simple makefile example, extracted from existing example
This commit is contained in:
@@ -0,0 +1,41 @@
|
|||||||
|
# ==========================================
|
||||||
|
# Unity Project - A Test Framework for C
|
||||||
|
# Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams
|
||||||
|
# [Released under MIT License. Please refer to license.txt for details]
|
||||||
|
# ==========================================
|
||||||
|
|
||||||
|
UNITY_ROOT=../..
|
||||||
|
C_COMPILER=gcc
|
||||||
|
TARGET_BASE1=test1
|
||||||
|
TARGET_BASE2=test2
|
||||||
|
ifeq ($(OS),Windows_NT)
|
||||||
|
TARGET_EXTENSION=.exe
|
||||||
|
else
|
||||||
|
TARGET_EXTENSION=.out
|
||||||
|
endif
|
||||||
|
TARGET1 = $(TARGET_BASE1)$(TARGET_EXTENSION)
|
||||||
|
TARGET2 = $(TARGET_BASE2)$(TARGET_EXTENSION)
|
||||||
|
SRC_FILES1=$(UNITY_ROOT)/src/unity.c src/ProductionCode.c test/TestProductionCode.c test/test_runners/TestProductionCode_Runner.c
|
||||||
|
SRC_FILES2=$(UNITY_ROOT)/src/unity.c src/ProductionCode2.c test/TestProductionCode2.c test/test_runners/TestProductionCode2_Runner.c
|
||||||
|
INC_DIRS=-Isrc -I$(UNITY_ROOT)/src
|
||||||
|
SYMBOLS=-DTEST
|
||||||
|
|
||||||
|
ifeq ($(OS),Windows_NT)
|
||||||
|
CLEANUP = del /F /Q $(TARGET1) && del /F /Q $(TARGET2)
|
||||||
|
else
|
||||||
|
CLEANUP = rm -f build/*.o ; rm -f $(TARGET1) ; rm -f $(TARGET2)
|
||||||
|
endif
|
||||||
|
|
||||||
|
all: clean default
|
||||||
|
|
||||||
|
default:
|
||||||
|
# ruby auto/generate_test_runner.rb test/TestProductionCode.c test/test_runners/TestProductionCode_Runner.c
|
||||||
|
# ruby auto/generate_test_runner.rb test/TestProductionCode2.c test/test_runners/TestProductionCode2_Runner.c
|
||||||
|
$(C_COMPILER) $(INC_DIRS) $(SYMBOLS) $(SRC_FILES1) -o $(TARGET1)
|
||||||
|
$(C_COMPILER) $(INC_DIRS) $(SYMBOLS) $(SRC_FILES2) -o $(TARGET2)
|
||||||
|
./$(TARGET1)
|
||||||
|
./$(TARGET2)
|
||||||
|
|
||||||
|
clean:
|
||||||
|
$(CLEANUP)
|
||||||
|
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
Example 1
|
||||||
|
=========
|
||||||
|
|
||||||
|
Close to the simplest possible example of Unity, using only basic features.
|
||||||
|
Run make to build & run the example tests.
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
|
||||||
|
#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;
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
|
||||||
|
int FindFunction_WhichIsBroken(int NumberToFind);
|
||||||
|
int FunctionWhichReturnsLocalVariable(void);
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
|
||||||
|
#include "ProductionCode2.h"
|
||||||
|
|
||||||
|
char* ThisFunctionHasNotBeenTested(int Poor, char* LittleFunction)
|
||||||
|
{
|
||||||
|
//Since There Are No Tests Yet, This Function Could Be Empty For All We Know.
|
||||||
|
// Which isn't terribly useful... but at least we put in a TEST_IGNORE so we won't forget
|
||||||
|
return (char*)0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
|
||||||
|
char* ThisFunctionHasNotBeenTested(int Poor, char* LittleFunction);
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
|
||||||
|
#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(1));
|
||||||
|
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());
|
||||||
|
}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
|
||||||
|
#include "ProductionCode2.h"
|
||||||
|
#include "unity.h"
|
||||||
|
|
||||||
|
/* These should be ignored because they are commented out in various ways:
|
||||||
|
#include "whatever.h"
|
||||||
|
*/
|
||||||
|
//#include "somethingelse.h"
|
||||||
|
|
||||||
|
void setUp(void)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void tearDown(void)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_IgnoredTest(void)
|
||||||
|
{
|
||||||
|
TEST_IGNORE_MESSAGE("This Test Was Ignored On Purpose");
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_AnotherIgnoredTest(void)
|
||||||
|
{
|
||||||
|
TEST_IGNORE_MESSAGE("These Can Be Useful For Leaving Yourself Notes On What You Need To Do Yet");
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_ThisFunctionHasNotBeenTested_NeedsToBeImplemented(void)
|
||||||
|
{
|
||||||
|
TEST_IGNORE(); //Like This
|
||||||
|
}
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
/* AUTOGENERATED FILE. DO NOT EDIT. */
|
||||||
|
#include "unity.h"
|
||||||
|
#include <setjmp.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
char MessageBuffer[50];
|
||||||
|
|
||||||
|
extern void setUp(void);
|
||||||
|
extern void tearDown(void);
|
||||||
|
|
||||||
|
extern void test_IgnoredTest(void);
|
||||||
|
extern void test_AnotherIgnoredTest(void);
|
||||||
|
extern void test_ThisFunctionHasNotBeenTested_NeedsToBeImplemented(void);
|
||||||
|
|
||||||
|
static void runTest(UnityTestFunction test)
|
||||||
|
{
|
||||||
|
if (TEST_PROTECT())
|
||||||
|
{
|
||||||
|
setUp();
|
||||||
|
test();
|
||||||
|
}
|
||||||
|
if (TEST_PROTECT() && !TEST_IS_IGNORED)
|
||||||
|
{
|
||||||
|
tearDown();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void resetTest()
|
||||||
|
{
|
||||||
|
tearDown();
|
||||||
|
setUp();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
Unity.TestFile = "test/TestProductionCode2.c";
|
||||||
|
UnityBegin();
|
||||||
|
|
||||||
|
// RUN_TEST calls runTest
|
||||||
|
RUN_TEST(test_IgnoredTest, 13);
|
||||||
|
RUN_TEST(test_AnotherIgnoredTest, 18);
|
||||||
|
RUN_TEST(test_ThisFunctionHasNotBeenTested_NeedsToBeImplemented, 23);
|
||||||
|
|
||||||
|
UnityEnd();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
/* AUTOGENERATED FILE. DO NOT EDIT. */
|
||||||
|
#include "unity.h"
|
||||||
|
#include <setjmp.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
char MessageBuffer[50];
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
static void runTest(UnityTestFunction test)
|
||||||
|
{
|
||||||
|
if (TEST_PROTECT())
|
||||||
|
{
|
||||||
|
setUp();
|
||||||
|
test();
|
||||||
|
}
|
||||||
|
if (TEST_PROTECT() && !TEST_IS_IGNORED)
|
||||||
|
{
|
||||||
|
tearDown();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void resetTest()
|
||||||
|
{
|
||||||
|
tearDown();
|
||||||
|
setUp();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
Unity.TestFile = "test/TestProductionCode.c";
|
||||||
|
UnityBegin();
|
||||||
|
|
||||||
|
// RUN_TEST calls runTest
|
||||||
|
RUN_TEST(test_FindFunction_WhichIsBroken_ShouldReturnZeroIfItemIsNotInList_WhichWorksEvenInOurBrokenCode, 20);
|
||||||
|
RUN_TEST(test_FindFunction_WhichIsBroken_ShouldReturnTheIndexForItemsInList_WhichWillFailBecauseOurFunctionUnderTestIsBroken, 30);
|
||||||
|
RUN_TEST(test_FunctionWhichReturnsLocalVariable_ShouldReturnTheCurrentCounterValue, 41);
|
||||||
|
RUN_TEST(test_FunctionWhichReturnsLocalVariable_ShouldReturnTheCurrentCounterValueAgain, 51);
|
||||||
|
RUN_TEST(test_FunctionWhichReturnsLocalVariable_ShouldReturnCurrentCounter_ButFailsBecauseThisTestIsActuallyFlawed, 57);
|
||||||
|
|
||||||
|
UnityEnd();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user