Original address: HTTPS://PHPHUB.ORG/TOPICS/25
Codeception Introduction
Codeception, in short, is divided into the following types of tests
- Acceptance Tests Acceptance Test
- Functional Tests Functional Test
- Unit Tests Units Test
Let's go through each of the advantages and disadvantages of each test.
Acceptance Tests Acceptance Test
Let's start with a scenario where the technician is finished, and its customers, product managers, or testers, how do they determine the availability of the product? In general, they are tested by performing the following steps:
- Open the browser;
- Enter the URL;
- See some information and make sure that this page is available;
- Click on a URL;
- Fill out the form, submit the form, see some information, and make sure that this feature is available
This test method we call 手动测试
, or 人工测试
, contrary to, the 自动化测试
codeception Acceptance Tests
will use the browser programming interface, do the above 人工测试
steps involved fully automated, greatly saving labor costs.
Post Code First
<? PHP $I New Acceptancetester ($scenario); $I->amonpage ('/'); $I->click (' Sign up '); $I Array (' username ' = ' milesdavis ', ' email ' = ' [email protected] '); $I->see (' Thank for Signing up! ');
Advantages
- Can be used to test any website;
- Completely browser-based, can test
Javascript
even AJAX requests;
- Can put the operation status to the Product manager or the customer to see, convincing people;
- Do not need redundant configuration, the application of the source code to modify the minimum requirements, good adaptability, can be used as the whole app to test, do not care about the internal implementation.
Disadvantages
- The test is slow because it needs to run on the browser and the real database;
- Compared to the unit test, do not complete the test, some small logic may be missed;
- Sometimes, when running, something is not controllable, because the browser rendering,
javascript
running, and sometimes unexpected situations occur.
- Again, this test will be very slow;
Functional Tests Functional Test
The functional test simulates a Web request (simulating variables such as $_get and $_post), sends it to the app, and the app returns the HTML results, which, during the test, can analyze and make decisions about the assert
returned data, and even check that the data is stored properly in the database.
Function testing needs to have a test environment, several well-known frameworks, like Laravel
there are ready-made Package
can be used for integration.
The following is a simple functional test:
<?PHP$I=NewFunctionaltester ($scenario);$I->amonpage ('/');$I->click (' Sign up ');$I->submitform (' #signup ',Array(' username ' = ' milesdavis ', ' email ' = ' [email protected] '));$I->see (' Thank for Signing up! ');$I->seeemailsent (' [email protected] ', ' Thank for registration ');$I->seeindatabase (' Users ',Array(' Email ' = ' [email protected] ');
Acceptance Tests 验收测试
This is similar to syntax because the test environment is integrated, allowing you to check email and database.
Advantages
Acceptance tests
similar, but less open browser to render, the speed is much faster;
- can provide more detailed analysis, such as database or email;
- The readability is very strong, although cannot let the tester see opens the browser to imitate the artificial test, but still can convince others;
- is more stable and only affects when large-scale code changes, or when the code is moved from one frame to another.
Disadvantages
- Unable to test JavaScript and Ajax;
- Because of the relatively simple use of code to simulate a browser request, the feasibility of testing, or integrity, will be relatively poor;
- Need a framework for support;
Unit Tests Units Test
Unit testing (also known as module testing, Unit testing) is a test that validates the correctness of the program module (the smallest unit of software design), and when the functional
acceptance
logic is not checked by the test, 最小单位
it is also possible to confirm some of the 单元测试
features hidden in the code is still available to 单元测试
eliminate the unreliability of the program unit.
Codeception's unit test function is based on PHPUnit
, you can write PHPUnit
the same test code, codeception can run.
Codeception PHPUnit
provides a range of tools to make unit testing simpler and more readable. Unit testing is the most complex and tedious test, and will follow the changes in business logic code, in the actual development of technical staff will often because of demand, Changes to the unit tests, improved readability, and ease of use can help people keep up with all the changes faster.
The following is a simpleintegration test (集成测试)
<? php function Testsavinguser () { $user = new User (); $user ->setname (' Miles ' $user ->setsurname (' Davis ' $user ->save (); $this ->assertequals (' Miles Davis ', ->getfullname ()); $this ->unittester->seeindatabase (' users ', array (' name ' = ' Miles ', ' surname ' = ' Davis '
Advantages
- The fastest test, of course, in the example code above, touched the database, or a little delay;
- Can cover the test to a particularly tricky program logic, which is
functional
or acceptance
cannot be done;
- Allows you to test the core code to determine the robustness of the core code;
- A program that writes unit tests is a good programmer.
Disadvantages
- Because it is a unit test, the code is divided into several small units to test separately, but the docking test between the units is not;
- The code changes are very sensitive, and many of the project's tests end up being useless because the test doesn't keep up with the changes in the business logic code.
PHP Behavioral Testing Tool Codeception (Introduction)