intro.bbdoc 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <img src="logo.png" align="right" />
  2. <p>A unit testing Module based loosely on JUnit.</p>
  3. To define a test :<br/>
  4. <ol>
  5. <li> implement a sub-Type of TTest</li>
  6. <li> initialize the user states by creating a Method tagged with `{before}`</li>
  7. <li> clean-up after a test by creating a Method tagged with `{after}`.</li>
  8. </ol>
  9. Here is an example:
  10. ```blitzmax
  11. Type MyTest Extends TTest
  12. Local value1:Int
  13. Local value2:Int
  14. Method setup() { before }
  15. value1 = 2
  16. value2 = 3
  17. End Method
  18. End Type
  19. ```
  20. For each test implement a @Method which interacts with the fixture. You tell MaxUnit that this is a test Method by
  21. tagging it with {test}. The Method can otherwise be called anything you like. It should take no parameters and not
  22. return anything.<br/>
  23. Verify the expected results with assertions specified by calling @assertTrue with a boolean.
  24. ```blitzmax
  25. Method testAdd() { test }
  26. Local result:Int = value1 + value2
  27. assertTrue(result = 5)
  28. End Method
  29. ```
  30. Finally, you can run the tests by creating an instance of the TTestSuite and calling its @run method.
  31. ```blitzmax
  32. New TTestSuite.run()
  33. ```
  34. The following code snippet is a complete example, including a failed test :<br/>
  35. You can open the source <a href="example.bmx">here</a>.
  36. ```blitzmax
  37. SuperStrict
  38. Import BRL.MaxUnit
  39. New TTestSuite.run()
  40. Type MyTest Extends TTest
  41. Field value1:Int
  42. Field value2:Int
  43. Method setup() { before }
  44. value1 = 2
  45. value2 = 3
  46. End Method
  47. Method testAdd() { test }
  48. Local result:Int = value1 + value2
  49. assertTrue(result = 5)
  50. End Method
  51. Method testMultiply() { test }
  52. Local result:Int = value1 * value2
  53. assertEqualsI(6, result)
  54. End Method
  55. Method testOops() { test }
  56. Local result:Int = value1 + value2
  57. assertEqualsI(2, result, "Just here to show a failed test...")
  58. End Method
  59. End Type
  60. ```
  61. The above program should produce the following output :
  62. ```
  63. [0] ..F
  64. There was 1 failure:
  65. 1) testOops
  66. assertEqualsI() : Just here to show a failed test... expected:<2> but was:<5>
  67. FAILURES!!!
  68. Tests run: 3, Failures: 1, Errors: 0
  69. Time: 0.0
  70. Process complete
  71. ```