RunCest.php 16 KB

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