autotestrunner.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. This class is just a convenience wrapper for using the actual TestHarness.
  3. */
  4. class AutotestRunner
  5. {
  6. private static bool m_IsRunning;
  7. private static bool m_IsDone;
  8. static bool IsRunning()
  9. {
  10. return m_IsRunning;
  11. }
  12. static bool IsDone()
  13. {
  14. return m_IsDone;
  15. }
  16. static void Start()
  17. {
  18. if (m_IsRunning)
  19. {
  20. ErrorEx("TestAutotest already running!");
  21. return;
  22. }
  23. AutoTestFixture.SetWorldName();
  24. // Enabled suites from JSON config provided on CLI (autotest param)
  25. set<string> enabledSuites = new set<string>();
  26. enabledSuites = AutotestConfigHandler.GetSuites();
  27. // Iterate through all suites and activate 'enabled' ones (list to be provided by config?)
  28. int numSuites = TestHarness.GetNSuites();
  29. if (numSuites == 0)
  30. {
  31. AutoTestFixture.LogRPT("No TestSuites to run.");
  32. m_IsDone = true;
  33. return;
  34. }
  35. for (int i = 0; i < numSuites; ++i)
  36. {
  37. TestSuite suite = TestHarness.GetSuite(i);
  38. bool isEnabled = enabledSuites.Find(suite.GetName()) != -1 && suite.IsEnabled();
  39. suite.SetEnabled(isEnabled);
  40. //AutoTestFixture.LogRPT(string.Format("Suite '%1' activation state set to: %2", suite.GetName(), isEnabled));
  41. }
  42. // Start running active TestSuite
  43. m_IsRunning = true;
  44. TestHarness.Begin();
  45. }
  46. static void Update(float deltaTime)
  47. {
  48. if (!m_IsRunning)
  49. return;
  50. if (!TestHarness.Finished())
  51. {
  52. bool isDone = TestHarness.Run();
  53. if (isDone)
  54. {
  55. string errorMessage;
  56. if (!AutoTestFixture.SaveXMLReport(TestHarness.Report(), errorMessage))
  57. AutoTestFixture.LogRPT(errorMessage);
  58. TestHarness.End();
  59. m_IsRunning = false;
  60. m_IsDone = true;
  61. }
  62. }
  63. }
  64. }
  65. /*
  66. Script wrapper for TestResultBase that allows script provided kind and message in elegant way.
  67. */
  68. class CustomResult : TestResultBase
  69. {
  70. private bool m_Success;
  71. private string m_FailureText;
  72. private string m_FailureKind;
  73. override bool Failure()
  74. {
  75. return !m_Success;
  76. }
  77. // string FailureTextNativeFormat(string type, string userTxt);
  78. // That will return the formatted string
  79. override string FailureText()
  80. {
  81. return string.Format("<failure type=\"%1\">%2</failure>", m_FailureKind, m_FailureText);
  82. }
  83. void CustomResult(bool success, string text = "User provided error!", string kind = "Failure")
  84. {
  85. m_Success = success;
  86. m_FailureText = text;
  87. m_FailureKind = kind;
  88. }
  89. }
  90. /* Suite is a collection of tests. */
  91. /*
  92. class FooTestSuite : TestSuite
  93. {
  94. // !!!
  95. // Be careful, if you leave the suite empty - no error is given and it will just ommit everything.
  96. // !!!
  97. [Step(EStage.Setup)]
  98. void FooSetup()
  99. {
  100. Print("FooTestSuite is setting up... Tests can commence now..");
  101. }
  102. [Step(EStage.TearDown)]
  103. void FooFinish()
  104. {
  105. Print("FooTestSuite is finishing up ... Tests are done..");
  106. }
  107. }
  108. */
  109. /* Test is registered within suite via the usage of the Test attribute. */
  110. /*
  111. [Test("FooTestSuite")]
  112. class FooTest1 : TestBase
  113. {
  114. private int m_Value;
  115. private const int k_TargetValue = 10;
  116. [Step(EStage.Setup)]
  117. void Setup()
  118. {
  119. m_Value = k_TargetValue;
  120. }
  121. [Step(EStage.Main)]
  122. void Main()
  123. {
  124. if ( m_Value != k_TargetValue )
  125. {
  126. SetResult( new CustomResult(false, string.Format("Expected value: %1, Actual value: %2", k_TargetValue, m_Value)) );
  127. }
  128. else
  129. {
  130. SetResult( new CustomResult(true, "Successfull!") );
  131. }
  132. }
  133. [Step(EStage.TearDown)]
  134. void Cleanup()
  135. {
  136. m_Value = 0;
  137. }
  138. }
  139. /* Test is registered within suite via the usage of the Test attribute. */
  140. /*
  141. [Test("FooTestSuite")]
  142. class FooTest2 : TestBase
  143. {
  144. private int m_Value;
  145. private const int k_TargetValue = 10;
  146. [Step(EStage.Setup)]
  147. void Setup()
  148. {
  149. m_Value = 11; // intentionally wrong
  150. }
  151. [Step(EStage.Main)]
  152. void Main()
  153. {
  154. if ( m_Value != k_TargetValue )
  155. {
  156. SetResult( new CustomResult(false, string.Format("Expected value: %1, Actual value: %2", k_TargetValue, m_Value)) );
  157. }
  158. else
  159. {
  160. SetResult( new CustomResult(true, "Successfull!") );
  161. }
  162. }
  163. [Step(EStage.TearDown)]
  164. void Cleanup()
  165. {
  166. m_Value = 0;
  167. }
  168. }
  169. class BarTestSuite : TestSuite
  170. {
  171. [Step(EStage.Setup)]
  172. void A();
  173. }
  174. [Test("BarTestSuite")]
  175. class BarTest1 : TestBase
  176. {
  177. [Step(EStage.Setup)]
  178. void Setup();
  179. [Step(EStage.Main)]
  180. void Main()
  181. {
  182. SetResult( new CustomResult(true, "Successfull!") );
  183. }
  184. [Step(EStage.TearDown)]
  185. void Cleanup();
  186. }
  187. */