TestProductionCode.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #include "ProductionCode.h"
  2. #include "unity.h"
  3. #include "unity_fixture.h"
  4. TEST_GROUP(ProductionCode);
  5. //sometimes you may want to get at local data in a module.
  6. //for example: If you plan to pass by reference, this could be useful
  7. //however, it should often be avoided
  8. extern int Counter;
  9. TEST_SETUP(ProductionCode)
  10. {
  11. //This is run before EACH TEST
  12. Counter = 0x5a5a;
  13. }
  14. TEST_TEAR_DOWN(ProductionCode)
  15. {
  16. }
  17. TEST(ProductionCode, FindFunction_WhichIsBroken_ShouldReturnZeroIfItemIsNotInList_WhichWorksEvenInOurBrokenCode)
  18. {
  19. //All of these should pass
  20. TEST_ASSERT_EQUAL(0, FindFunction_WhichIsBroken(78));
  21. TEST_ASSERT_EQUAL(0, FindFunction_WhichIsBroken(1));
  22. TEST_ASSERT_EQUAL(0, FindFunction_WhichIsBroken(33));
  23. TEST_ASSERT_EQUAL(0, FindFunction_WhichIsBroken(999));
  24. TEST_ASSERT_EQUAL(0, FindFunction_WhichIsBroken(-1));
  25. }
  26. TEST(ProductionCode, FindFunction_WhichIsBroken_ShouldReturnTheIndexForItemsInList_WhichWillFailBecauseOurFunctionUnderTestIsBroken)
  27. {
  28. // You should see this line fail in your test summary
  29. TEST_ASSERT_EQUAL(1, FindFunction_WhichIsBroken(34));
  30. // Notice the rest of these didn't get a chance to run because the line above failed.
  31. // Unit tests abort each test function on the first sign of trouble.
  32. // Then NEXT test function runs as normal.
  33. TEST_ASSERT_EQUAL(8, FindFunction_WhichIsBroken(8888));
  34. }
  35. TEST(ProductionCode, FunctionWhichReturnsLocalVariable_ShouldReturnTheCurrentCounterValue)
  36. {
  37. //This should be true because setUp set this up for us before this test
  38. TEST_ASSERT_EQUAL_HEX(0x5a5a, FunctionWhichReturnsLocalVariable());
  39. //This should be true because we can still change our answer
  40. Counter = 0x1234;
  41. TEST_ASSERT_EQUAL_HEX(0x1234, FunctionWhichReturnsLocalVariable());
  42. }
  43. TEST(ProductionCode, FunctionWhichReturnsLocalVariable_ShouldReturnTheCurrentCounterValueAgain)
  44. {
  45. //This should be true again because setup was rerun before this test (and after we changed it to 0x1234)
  46. TEST_ASSERT_EQUAL_HEX(0x5a5a, FunctionWhichReturnsLocalVariable());
  47. }
  48. TEST(ProductionCode, FunctionWhichReturnsLocalVariable_ShouldReturnCurrentCounter_ButFailsBecauseThisTestIsActuallyFlawed)
  49. {
  50. //Sometimes you get the test wrong. When that happens, you get a failure too... and a quick look should tell
  51. // you what actually happened...which in this case was a failure to setup the initial condition.
  52. TEST_ASSERT_EQUAL_HEX(0x1234, FunctionWhichReturnsLocalVariable());
  53. }