Jump to content

Perform functional tests


rick645

Recommended Posts

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 by rick645
Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

I had a look but I didn't really understand how to use it
https://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....

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.