rick645 Posted September 18, 2023 Share Posted September 18, 2023 (edited) As we know, to perform the unit tests in general is used phpunit https://phpunit.de/ But for functional tests? For example, let's consider the following script + cat bin/example-script #!/usr/bin/php <?php require "src/App.php"; /** * @return string Application output. */ function main(): string { $app = new App; // --- BEGIN CONFIG --- $app->setProperty1( // ... ); $app->setProperty2( // ... ); $app->setProperty3( // ... ); // --- END CONFIG --- $output = $app->run(); # RETURN: "Helo world!!!" return $output; } echo main() . PHP_EOL; # ECHO: "Helo world!!!" . PHP_EOL exit(123); # EXIT CODE: 123 We highlight some lines + grep '# ' bin/example-script $output = $app->run(); # RETURN: "Helo world!!!" echo main() . PHP_EOL; # ECHO: "Helo world!!!" . PHP_EOL exit(123); # EXIT CODE: 123 Request 1 I want to test that the output is the desired one: "Helo world!!!" . PHP_EOL Request 2 I want to test that the exit code is the desired one: 123 How can it be done? Is there a recommended practice? Edited September 18, 2023 by rick645 Quote Link to comment https://forums.phpfreaks.com/topic/317296-perform-functional-tests/ Share on other sites More sharing options...
rick645 Posted September 25, 2023 Author Share Posted September 25, 2023 UPPPPPPPPPPPP Quote Link to comment https://forums.phpfreaks.com/topic/317296-perform-functional-tests/#findComment-1612102 Share on other sites More sharing options...
gizmola Posted September 27, 2023 Share Posted September 27, 2023 In general, functional testing of web apps involves some sort of tool that can either simulate a browser (Codeception, Testing Library) or integrate with one (Selenium, Watir). They are specifically built to deal with browser clients. For testing of CLI programs, there aren't a lot of options out there that I'm familiar with, but one that you can look at is cli-testing-library Confusingly there is another library worth looking at with the same name. Either library should allow you to write and run functional tests for the outcomes you described, but also provide ways for providing input, options and interaction. Let us know if one or the other worked out for you. Quote Link to comment https://forums.phpfreaks.com/topic/317296-perform-functional-tests/#findComment-1612139 Share on other sites More sharing options...
rick645 Posted October 2, 2023 Author Share Posted October 2, 2023 I had a look but I didn't really understand how to use ithttps://github.com/gmrchk/cli-testing-library#usage You need to create a file of this type import { prepareEnvironment } from '@gmrchk/cli-testing-library'; describe('My CLI', () => { it('program runs successfully', async () => { const { execute, cleanup } = await prepareEnvironment(); const { code } = await execute( 'node', './my-cli.js --help' ); expect(code).toBe(0); await cleanup(); }); }); and then where should we save it? Since they are tools written in JS (if I'm not mistaken), do the tests have to be launched from the browser? PS Do you know anything about this topic? https://forums.phpfreaks.com/topic/317297-vscode-docblock-and-tag-see/#comment-1612101 No one has responded yet.... Quote Link to comment https://forums.phpfreaks.com/topic/317296-perform-functional-tests/#findComment-1612216 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.