RunCest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. <?php
  2. class RunCest
  3. {
  4. public function _before(\CliGuy $I)
  5. {
  6. $I->amInPath('tests/data/sandbox');
  7. }
  8. public function runOneFile(\CliGuy $I)
  9. {
  10. $I->wantTo('execute one test');
  11. $I->executeCommand('run tests/dummy/FileExistsCept.php');
  12. $I->seeInShellOutput("OK (");
  13. }
  14. /**
  15. * @group reports
  16. * @group core
  17. *
  18. * @param CliGuy $I
  19. */
  20. public function runHtml(\CliGuy $I)
  21. {
  22. $I->wantTo('execute tests with html output');
  23. $I->executeCommand('run dummy --html');
  24. $I->seeFileFound('report.html', 'tests/_output');
  25. }
  26. /**
  27. * @group reports
  28. *
  29. * @param CliGuy $I
  30. */
  31. public function runJsonReport(\CliGuy $I)
  32. {
  33. $I->wantTo('check json reports');
  34. $I->executeCommand('run dummy --json');
  35. $I->seeFileFound('report.json', 'tests/_output');
  36. $I->seeInThisFile('"suite":');
  37. $I->seeInThisFile('"dummy"');
  38. }
  39. /**
  40. * @group reports
  41. *
  42. * @param CliGuy $I
  43. */
  44. public function runTapReport(\CliGuy $I)
  45. {
  46. $I->wantTo('check tap reports');
  47. $I->executeCommand('run dummy --tap');
  48. $I->seeFileFound('report.tap.log', 'tests/_output');
  49. }
  50. /**
  51. * @group reports
  52. *
  53. * @param CliGuy $I
  54. */
  55. public function runXmlReport(\CliGuy $I)
  56. {
  57. $I->wantTo('check xml reports');
  58. $I->executeCommand('run dummy --xml');
  59. $I->seeFileFound('report.xml', 'tests/_output');
  60. $I->seeInThisFile('<?xml');
  61. $I->seeInThisFile('<testsuite name="dummy"');
  62. $I->seeInThisFile('<testcase name="FileExists"');
  63. $I->seeInThisFile('feature="');
  64. }
  65. /**
  66. * @group reports
  67. * @param CliGuy $I
  68. */
  69. public function runXmlReportsInStrictMode(\CliGuy $I)
  70. {
  71. $I->wantTo('check xml in strict mode');
  72. $I->executeCommand('run dummy --xml -c codeception_strict_xml.yml');
  73. $I->seeFileFound('report.xml', 'tests/_output');
  74. $I->seeInThisFile('<?xml');
  75. $I->seeInThisFile('<testsuite name="dummy"');
  76. $I->seeInThisFile('<testcase name="FileExists"');
  77. $I->dontSeeInThisFile('feature="');
  78. }
  79. /**
  80. * @group reports
  81. *
  82. * @param CliGuy $I
  83. */
  84. public function runReportMode(\CliGuy $I)
  85. {
  86. $I->wantTo('try the reporting mode');
  87. $I->executeCommand('run dummy --report');
  88. $I->seeInShellOutput('FileExistsCept');
  89. $I->seeInShellOutput('........Ok');
  90. }
  91. /**
  92. * @group reports
  93. *
  94. * @param CliGuy $I
  95. */
  96. public function runCustomReport(\CliGuy $I)
  97. {
  98. $I->wantTo('try the reporting mode');
  99. $I->executeCommand('run dummy --report -c codeception_custom_report.yml');
  100. $I->seeInShellOutput('FileExistsCept: Check config exists');
  101. $I->dontSeeInShellOutput('Ok');
  102. }
  103. public function runOneGroup(\CliGuy $I)
  104. {
  105. $I->executeCommand('run skipped -g notorun');
  106. $I->seeInShellOutput('Skipped Tests (1)');
  107. $I->seeInShellOutput("IncompleteMeCept");
  108. $I->dontSeeInShellOutput("SkipMeCept");
  109. }
  110. public function skipRunOneGroup(\CliGuy $I)
  111. {
  112. $I->executeCommand('run skipped --skip-group notorun');
  113. $I->seeInShellOutput('Skipped Tests (2)');
  114. $I->seeInShellOutput("SkipMeCept");
  115. $I->dontSeeInShellOutput("IncompleteMeCept");
  116. }
  117. public function skipGroupOfCest(\CliGuy $I)
  118. {
  119. $I->executeCommand('run dummy');
  120. $I->seeInShellOutput('Optimistic');
  121. $I->seeInShellOutput('Dummy Tests (6)');
  122. $I->executeCommand('run dummy --skip-group ok');
  123. $I->seeInShellOutput('Pessimistic');
  124. $I->seeInShellOutput('Dummy Tests (5)');
  125. $I->dontSeeInShellOutput('Optimistic');
  126. }
  127. public function runTwoSuites(\CliGuy $I)
  128. {
  129. $I->executeCommand('run skipped,dummy --no-exit');
  130. $I->seeInShellOutput("Skipped Tests (3)");
  131. $I->seeInShellOutput("Dummy Tests (6)");
  132. $I->dontSeeInShellOutput("Remote Tests");
  133. }
  134. public function skipSuites(\CliGuy $I)
  135. {
  136. $I->executeCommand(
  137. 'run dummy --skip skipped --skip remote --skip remote_server --skip order --skip unit '
  138. . '--skip powers --skip math --skip messages'
  139. );
  140. $I->seeInShellOutput("Dummy Tests");
  141. $I->dontSeeInShellOutput("Remote Tests");
  142. $I->dontSeeInShellOutput("Remote_server Tests");
  143. $I->dontSeeInShellOutput("Order Tests");
  144. }
  145. public function runOneTestFromUnit(\CliGuy $I)
  146. {
  147. $I->executeCommand('run tests/dummy/AnotherTest.php:testFirst');
  148. $I->seeInShellOutput("AnotherTest: First");
  149. $I->seeInShellOutput('OK');
  150. $I->dontSeeInShellOutput('AnotherTest: Second');
  151. }
  152. public function runOneTestFromCest(\CliGuy $I)
  153. {
  154. $I->executeCommand('run tests/dummy/AnotherCest.php:optimistic');
  155. $I->seeInShellOutput("Optimistic");
  156. $I->dontSeeInShellOutput('Pessimistic');
  157. }
  158. public function runTestWithDataProviders(\CliGuy $I)
  159. {
  160. $I->executeCommand('run tests/unit/DataProvidersTest.php');
  161. $I->seeInShellOutput('Is triangle | "real triangle"');
  162. $I->seeInShellOutput('Is triangle | #0');
  163. $I->seeInShellOutput('Is triangle | #1');
  164. $I->seeInShellOutput('DataProvidersTest');
  165. $I->seeInShellOutput("OK");
  166. }
  167. public function runOneGroupWithDataProviders(\CliGuy $I)
  168. {
  169. $I->executeCommand('run unit -g data-providers');
  170. $I->seeInShellOutput('Is triangle | "real triangle"');
  171. $I->seeInShellOutput('Is triangle | #0');
  172. $I->seeInShellOutput('Is triangle | #1');
  173. $I->seeInShellOutput('DataProvidersTest');
  174. $I->seeInShellOutput("OK");
  175. }
  176. public function runTestWithFailFast(\CliGuy $I)
  177. {
  178. $I->executeCommand('run unit --skip-group error --no-exit');
  179. $I->seeInShellOutput('FailingTest: Me');
  180. $I->seeInShellOutput("PassingTest: Me");
  181. $I->executeCommand('run unit --fail-fast --skip-group error --no-exit');
  182. $I->seeInShellOutput('There was 1 failure');
  183. $I->dontSeeInShellOutput("PassingTest: Me");
  184. }
  185. public function runWithCustomOutputPath(\CliGuy $I)
  186. {
  187. $I->executeCommand('run dummy --xml myverycustom.xml --html myownhtmlreport.html');
  188. $I->seeFileFound('myverycustom.xml', 'tests/_output');
  189. $I->seeInThisFile('<?xml');
  190. $I->seeInThisFile('<testsuite name="dummy"');
  191. $I->seeInThisFile('<testcase name="FileExists"');
  192. $I->seeFileFound('myownhtmlreport.html', 'tests/_output');
  193. $I->dontSeeFileFound('report.xml', 'tests/_output');
  194. $I->dontSeeFileFound('report.html', 'tests/_output');
  195. }
  196. public function runTestsWithDependencyInjections(\CliGuy $I)
  197. {
  198. $I->executeCommand('run math');
  199. $I->seeInShellOutput('MathCest: Test addition');
  200. $I->seeInShellOutput('MathCest: Test subtraction');
  201. $I->seeInShellOutput('MathCest: Test square');
  202. $I->seeInShellOutput('MathTest: All');
  203. $I->seeInShellOutput('OK (');
  204. $I->dontSeeInShellOutput('fail');
  205. $I->dontSeeInShellOutput('error');
  206. }
  207. public function runErrorTest(\CliGuy $I)
  208. {
  209. $I->executeCommand('run unit ErrorTest --no-exit');
  210. $I->seeInShellOutput('There was 1 error');
  211. $I->seeInShellOutput('Array to string conversion');
  212. $I->seeInShellOutput('ErrorTest.php:9');
  213. }
  214. public function runTestWithException(\CliGuy $I)
  215. {
  216. $I->executeCommand('run unit ExceptionTest --no-exit -v');
  217. $I->seeInShellOutput('There was 1 error');
  218. $I->seeInShellOutput('Helllo!');
  219. $I->expect('Exceptions are not wrapped into ExceptionWrapper');
  220. $I->dontSeeInShellOutput('PHPUnit_Framework_ExceptionWrapper');
  221. $I->seeInShellOutput('RuntimeException');
  222. }
  223. public function runTestsWithSteps(\CliGuy $I)
  224. {
  225. $I->executeCommand('run scenario SuccessCept --steps');
  226. $I->seeInShellOutput(<<<EOF
  227. Scenario --
  228. I am in path "."
  229. I see file found "scenario.suite.yml"
  230. PASSED
  231. EOF
  232. );
  233. }
  234. /**
  235. * @param CliGuy $I
  236. */
  237. public function runTestWithFailedScenario(\CliGuy $I, $scenario)
  238. {
  239. if (!extension_loaded('xdebug') && !defined('HHVM_VERSION')) {
  240. $scenario->skip("Xdebug not loaded");
  241. }
  242. $I->executeCommand('run scenario FailedCept --steps --no-exit');
  243. $I->seeInShellOutput(<<<EOF
  244. FailedCept: Fail when file is not found
  245. Signature: FailedCept
  246. Test: tests/scenario/FailedCept.php
  247. Scenario --
  248. I am in path "."
  249. I see file found "games.zip"
  250. FAIL
  251. EOF
  252. );
  253. $I->expect('to see scenario trace');
  254. $I->seeInShellOutput(<<<EOF
  255. Scenario Steps:
  256. 2. \$I->seeFileFound("games.zip") at tests/scenario/FailedCept.php:5
  257. 1. \$I->amInPath(".") at tests/scenario/FailedCept.php:4
  258. EOF
  259. );
  260. }
  261. /**
  262. * @param CliGuy $I
  263. */
  264. public function runTestWithSubSteps(\CliGuy $I, $scenario)
  265. {
  266. if (!extension_loaded('xdebug') && !defined('HHVM_VERSION')) {
  267. $scenario->skip("Xdebug not loaded");
  268. }
  269. $file = "codeception".DIRECTORY_SEPARATOR."c3";
  270. $I->executeCommand('run scenario SubStepsCept --steps');
  271. $I->seeInShellOutput(<<<EOF
  272. Scenario --
  273. I am in path "."
  274. I see code coverage files are present
  275. EOF
  276. );
  277. // I split this assertion into two, because extra space is printed after "present" on HHVM
  278. $I->seeInShellOutput(<<<EOF
  279. I see file found "c3.php"
  280. I see file found "composer.json"
  281. I see in this file "$file"
  282. EOF
  283. );
  284. }
  285. public function runDependentCest(CliGuy $I)
  286. {
  287. $I->executeCommand('run order DependentCest --no-exit');
  288. $I->seeInShellOutput('Skipped: 1');
  289. }
  290. public function runDependentTest(CliGuy $I)
  291. {
  292. $I->executeCommand('run unit DependsTest --no-exit');
  293. $I->seeInShellOutput('Skipped: 1');
  294. $I->executeCommand('run unit --no-exit');
  295. $I->seeInShellOutput('Skipped: 2');
  296. }
  297. public function runGherkinTest(CliGuy $I)
  298. {
  299. $I->executeCommand('run scenario File.feature --steps');
  300. $I->seeInShellOutput(<<<EOF
  301. In order to test a feature
  302. As a user
  303. I need to be able to see output
  304. EOF
  305. );
  306. $I->seeInShellOutput('Given i have terminal opened');
  307. $I->seeInShellOutput('When i am in current directory');
  308. $I->seeInShellOutput('Then there is a file "scenario.suite.yml"');
  309. $I->seeInShellOutput('And there are keywords in "scenario.suite.yml"');
  310. $I->seeInShellOutput(<<<EOF
  311. | class_name | ScenarioGuy |
  312. | enabled | Filesystem |
  313. EOF
  314. );
  315. $I->seeInShellOutput('PASSED');
  316. }
  317. public function runIncompleteGherkinTest(CliGuy $I)
  318. {
  319. $I->executeCommand('run scenario File.feature -v');
  320. $I->seeInShellOutput('OK, but incomplete');
  321. $I->seeInShellOutput('Step definition for `I have only idea of what\'s going on here` not found in contexts');
  322. }
  323. public function runGherkinScenarioWithMultipleStepDefinitions(CliGuy $I)
  324. {
  325. $I->executeCommand('run scenario "File.feature:Check file once more" --steps');
  326. $I->seeInShellOutput('When there is a file "scenario.suite.yml"');
  327. $I->seeInShellOutput('Then i see file "scenario.suite.yml"');
  328. $I->dontSeeInShellOutput('Step definition for `I see file "scenario.suite.yml"` not found in contexts');
  329. $I->seeInShellOutput('PASSED');
  330. }
  331. public function runGherkinScenarioOutline(CliGuy $I)
  332. {
  333. $I->executeCommand('run scenario FileExamples.feature -v');
  334. $I->seeInShellOutput('OK (3 tests');
  335. }
  336. /**
  337. * @param CliGuy $I
  338. * @after checkExampleFiles
  339. */
  340. public function runTestWithAnnotationExamples(CliGuy $I)
  341. {
  342. $I->executeCommand('run scenario ExamplesCest:filesExistsAnnotation --steps');
  343. }
  344. /**
  345. * @param CliGuy $I
  346. * @after checkExampleFiles
  347. */
  348. public function runTestWithJsonExamples(CliGuy $I)
  349. {
  350. $I->executeCommand('run scenario ExamplesCest:filesExistsByJson --steps');
  351. }
  352. /**
  353. * @param CliGuy $I
  354. * @after checkExampleFiles
  355. */
  356. public function runTestWithArrayExamples(CliGuy $I)
  357. {
  358. $I->executeCommand('run scenario ExamplesCest:filesExistsByArray --steps');
  359. }
  360. protected function checkExampleFiles(CliGuy $I)
  361. {
  362. $I->seeInShellOutput('OK (3 tests');
  363. $I->seeInShellOutput('I see file found "scenario.suite.yml"');
  364. $I->seeInShellOutput('I see file found "dummy.suite.yml"');
  365. $I->seeInShellOutput('I see file found "unit.suite.yml"');
  366. }
  367. public function runTestWithComplexExample(CliGuy $I)
  368. {
  369. $I->executeCommand('run scenario ExamplesCest:filesExistsComplexJson --debug');
  370. $I->seeInShellOutput('Files exists complex json | {"path":"."');
  371. $I->seeInShellOutput('OK (1 test');
  372. $I->seeInShellOutput('I see file found "scenario.suite.yml"');
  373. $I->seeInShellOutput('I see file found "dummy.suite.yml"');
  374. $I->seeInShellOutput('I see file found "unit.suite.yml"');
  375. }
  376. public function overrideConfigOptionsToChangeReporter(CliGuy $I)
  377. {
  378. if (!class_exists('PHPUnit_Util_Log_TeamCity')) {
  379. throw new \Codeception\Exception\Skip('Reporter does not exist for this PHPUnit version');
  380. }
  381. $I->executeCommand('run scenario --report -o "reporters: report: PHPUnit_Util_Log_TeamCity" --no-exit');
  382. $I->seeInShellOutput('##teamcity[testStarted');
  383. $I->dontSeeInShellOutput('............Ok');
  384. }
  385. public function overrideModuleOptions(CliGuy $I)
  386. {
  387. $I->executeCommand('run powers --no-exit');
  388. $I->seeInShellOutput('FAILURES');
  389. $I->executeCommand('run powers -o "modules: config: PowerHelper: has_power: true" --no-exit');
  390. $I->dontSeeInShellOutput('FAILURES');
  391. }
  392. public function runTestWithAnnotationExamplesFromGroupFileTest(CliGuy $I)
  393. {
  394. $I->executeCommand('run scenario -g groupFileTest1 --steps');
  395. $I->seeInShellOutput('OK (3 tests');
  396. }
  397. public function testsWithConditionalFails(CliGuy $I)
  398. {
  399. $I->executeCommand('run scenario ConditionalCept --no-exit');
  400. $I->seeInShellOutput('There were 3 failures');
  401. $I->seeInShellOutput('Fail File "not-a-file" not found');
  402. $I->seeInShellOutput('Fail File "not-a-dir" not found');
  403. $I->seeInShellOutput('Fail File "nothing" not found');
  404. }
  405. public function runTestWithAnnotationDataprovider(CliGuy $I)
  406. {
  407. $I->executeCommand('run scenario -g dataprovider --steps');
  408. $I->seeInShellOutput('OK (15 tests');
  409. }
  410. }