Simulated product dispenser
Fork of mbed-cloud-workshop-connect-HTS221 by
TestProductionCode.c
00001 #include "ProductionCode.h" 00002 #include "unity.h" 00003 #include "unity_fixture.h" 00004 00005 TEST_GROUP(ProductionCode); 00006 00007 //sometimes you may want to get at local data in a module. 00008 //for example: If you plan to pass by reference, this could be useful 00009 //however, it should often be avoided 00010 extern int Counter; 00011 00012 TEST_SETUP(ProductionCode) 00013 { 00014 //This is run before EACH TEST 00015 Counter = 0x5a5a; 00016 } 00017 00018 TEST_TEAR_DOWN(ProductionCode) 00019 { 00020 } 00021 00022 TEST(ProductionCode, FindFunction_WhichIsBroken_ShouldReturnZeroIfItemIsNotInList_WhichWorksEvenInOurBrokenCode) 00023 { 00024 //All of these should pass 00025 TEST_ASSERT_EQUAL(0, FindFunction_WhichIsBroken(78)); 00026 TEST_ASSERT_EQUAL(0, FindFunction_WhichIsBroken(1)); 00027 TEST_ASSERT_EQUAL(0, FindFunction_WhichIsBroken(33)); 00028 TEST_ASSERT_EQUAL(0, FindFunction_WhichIsBroken(999)); 00029 TEST_ASSERT_EQUAL(0, FindFunction_WhichIsBroken(-1)); 00030 } 00031 00032 TEST(ProductionCode, FindFunction_WhichIsBroken_ShouldReturnTheIndexForItemsInList_WhichWillFailBecauseOurFunctionUnderTestIsBroken) 00033 { 00034 // You should see this line fail in your test summary 00035 TEST_ASSERT_EQUAL(1, FindFunction_WhichIsBroken(34)); 00036 00037 // Notice the rest of these didn't get a chance to run because the line above failed. 00038 // Unit tests abort each test function on the first sign of trouble. 00039 // Then NEXT test function runs as normal. 00040 TEST_ASSERT_EQUAL(8, FindFunction_WhichIsBroken(8888)); 00041 } 00042 00043 TEST(ProductionCode, FunctionWhichReturnsLocalVariable_ShouldReturnTheCurrentCounterValue) 00044 { 00045 //This should be true because setUp set this up for us before this test 00046 TEST_ASSERT_EQUAL_HEX(0x5a5a, FunctionWhichReturnsLocalVariable()); 00047 00048 //This should be true because we can still change our answer 00049 Counter = 0x1234; 00050 TEST_ASSERT_EQUAL_HEX(0x1234, FunctionWhichReturnsLocalVariable()); 00051 } 00052 00053 TEST(ProductionCode, FunctionWhichReturnsLocalVariable_ShouldReturnTheCurrentCounterValueAgain) 00054 { 00055 //This should be true again because setup was rerun before this test (and after we changed it to 0x1234) 00056 TEST_ASSERT_EQUAL_HEX(0x5a5a, FunctionWhichReturnsLocalVariable()); 00057 } 00058 00059 TEST(ProductionCode, FunctionWhichReturnsLocalVariable_ShouldReturnCurrentCounter_ButFailsBecauseThisTestIsActuallyFlawed) 00060 { 00061 //Sometimes you get the test wrong. When that happens, you get a failure too... and a quick look should tell 00062 // you what actually happened...which in this case was a failure to setup the initial condition. 00063 TEST_ASSERT_EQUAL_HEX(0x1234, FunctionWhichReturnsLocalVariable()); 00064 }
Generated on Tue Jul 12 2022 19:12:16 by 1.7.2