Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test #904

Merged
merged 11 commits into from
Apr 17, 2023
Merged

Add test #904

merged 11 commits into from
Apr 17, 2023

Conversation

jhdxr
Copy link
Contributor

@jhdxr jhdxr commented Apr 14, 2023

TL;DR
This PR adds Github Action enabled tests with coverage.


This PR adds:

  1. Pest for testing and mockery for mock as dev dependency.
  2. Github Action integration, with PHP 8.1 and 8.2 on master branch and PR.
  3. Test converage
  4. Some test cases.

Currently there is only one completed test case for demonstration purpose. I may add more if maintainers are interested in this PR.

@walkor
Copy link
Owner

walkor commented Apr 15, 2023

I may add more if maintainers are interested in this PR.

Yes, unit testing is currently urgently needed by Workman. Thank you very much for your contribution.

@joanhey
Copy link
Contributor

joanhey commented Apr 15, 2023

Yes, we need tests. And I really like to use Pest.

But we don't need too many unit tests, only for some classes.
Because unit tests are tied to code and classes. And with any refactor, than it will be more, we need to change the unit tests.
Webman really need a lot of unit tests, but not Workerman.

We need more URGENT to have functional tests.

It's better to test the behavior than the code in Workerman.
So we can test any version, without touch the tests. And touch and refactor the code freely.
Also they can be used in other projects: Adapterman, Amp, ReactPHP, ... that will feedback with more tests.

Apache and Nginx use functional tests

Apache:

Nginx:

Also Ngx-php use the functional test from Nginx: https://github.com/rryqszq4/ngx-php/blob/master/t/001-hello.t

Both use perl, because are veterans projects, and at that time is what they have at hand.

PHP use functional tests

A better example, is the same PHP.

We need an slight modified .phpt tests. And try to be language agnostic.
They are easier to create, than unit test. They don't need to know the code or the test system. And more people will help creating tests.
And will be in the same GIT repo.

And I think more in have an echo server in some parts, like in https://httpbin.org

Other options

We can start fast and use ready made solutions, but they have disadvantages too.

Thunder Client
https://www.thunderclient.com/
Easy, with GUI, anybody can create and test in the same Vscode. Can import collections from Postman, Insomnia, ...
With an easy tests system and can save the files in GIT too.

Negative:

  • Use json files, and with a lot of tests, are very large
  • Difficult to manage commit changes with these large json
  • We need another languaje: js and nodejs.

Rest client
https://marketplace.visualstudio.com/items?itemName=humao.rest-client

Easy to create, and with autocomplete in IDEs of .rest and .http files (both have RFCs)
image

Very good way, perhaps better than .phpt for Workeman. Also separated in dirs and easy to review changes in commits.
And I did some preliminary tests, and we can mix .rest with .phpt. We can use .rest files with --EXPECT--, --EXPECTHEADERS--, ...

Negative:

  • Don't have tests

What is better to use?
Any feedback is welcome.

Link to issue #833

@joanhey joanhey mentioned this pull request Apr 15, 2023
@joanhey
Copy link
Contributor

joanhey commented Apr 15, 2023

I copied the comment to the issue, so will be better to continue the conversation.

@jhdxr jhdxr force-pushed the feature/tests branch 2 times, most recently from f24d86f to 098c8b6 Compare April 16, 2023 21:09
@jhdxr
Copy link
Contributor Author

jhdxr commented Apr 16, 2023

Because unit tests are tied to code and classes. And with any refactor, than it will be more, we need to change the unit tests.

Yes and no. The unit tests I wrote are against public methods which usually should remain stable.

It's better to test the behavior than the code in Workerman.

While I still believe unit tests are necessary and important, I also agree behavior tests are equally needed.

I tried to add one for general udp connection which turns out not feasible at this moment, as pest doesn't support process isolation while the only way to stop/kill workerman workers is killing the whole process. ← Please correct me if I'm wrong.


I'll mark this PR as ready for review, as the key point of this PR is to introduce testing tools with CI. I can remove all test cases or move them to another PR if necessary.

The author of pest doesn't seem to have interest to add process isolation although he is open to a PR. There was one but abandoned. I think I will spend some time to look into that, or if any solution on workerman side is also helpful.

@jhdxr jhdxr marked this pull request as ready for review April 16, 2023 21:45
@walkor walkor merged commit d707ff9 into walkor:master Apr 17, 2023
@walkor
Copy link
Owner

walkor commented Apr 17, 2023

Yes, both unit testing and behavioral testing are required. Thank you for jhdxr's PR and joanhey's detailed discussion. I have merged the PR on unit testing. I am not familiar with testing but using Perl may not seem like a good idea, as most developers are not familiar with it now. Perhaps we can also use PHPUnit for behavioral testing?

@joanhey
Copy link
Contributor

joanhey commented Apr 17, 2023

The .phpt in php use PHP.
https://github.com/php/php-src/blob/master/run-tests.php

@walkor
Copy link
Owner

walkor commented Apr 17, 2023

Yes, .phpt is ok.

@jhdxr
Copy link
Contributor Author

jhdxr commented Apr 17, 2023

pest is built on top of PHPUnit, I chose pest because of personal habit (light and fluent in writing testing cases, and I thought it can fulfill the needs). So yes we can definitely switch to PHPUnit.

.phpt from php/php-src is a bit different. It's designed to test the interpreter itself. All test cases are small snippets and comparison are done (EXPECT/EXPECTF) on a string basis. It may not be a good choice here because it will be troublesome if we want to test anything beyond the text value (e.g. type check), setting up global/resusable setup and teardown methods, or mocking certain class/interface.

@jhdxr jhdxr mentioned this pull request Apr 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants