How does a data-dependent project perform unit testing?

Source: Internet
Author: User
Doing a project is bound to be a unit test to ensure the robustness of the program to ensure that the version iterations function properly. But if a program, such as a CMS, is itself dependent on the database, how should you do unit testing without destroying the database?

Example:
Visit the Id=1 page, first check

sqlSELECT * FROM `data` WHERE `ID` = 1

, and assign them to, and $data->ID ensure that the $data->Title values of these properties are correct .
Then, based on some information $data->Hash , for example $data->Hash = md5($data->ID) , the values of these calculations should also be normal .
Next, delete id=1 the article that you want to

sqlDELETE FROM `data` WHERE `ID` = 1

, you must ensure that the row where the ID is located is deleted .
Second, the background to publish an article, you must make the database in more than the ID=2 content of the set value of the article. However, HTML code content does not have to be the same, allowing for things like extra spaces and line breaks.
It also requires a mock-XML-RPC protocol to publish an article.

Like the above content, how should you do unit testing?

Question of the same name: http://www.zhihu.com/question/28278690

Reply content:

Doing a project is bound to be a unit test to ensure the robustness of the program to ensure that the version iterations function properly. But if a program, such as a CMS, is itself dependent on the database, how should you do unit testing without destroying the database?

Example:
Visit the Id=1 page, first check

sqlSELECT * FROM `data` WHERE `ID` = 1

, and assign them to, and $data->ID ensure that the $data->Title values of these properties are correct .
Then, based on some information $data->Hash , for example $data->Hash = md5($data->ID) , the values of these calculations should also be normal .
Next, delete id=1 the article that you want to

sqlDELETE FROM `data` WHERE `ID` = 1

, you must ensure that the row where the ID is located is deleted .
Second, the background to publish an article, you must make the database in more than the ID=2 content of the set value of the article. However, HTML code content does not have to be the same, allowing for things like extra spaces and line breaks.
It also requires a mock-XML-RPC protocol to publish an article.

Like the above content, how should you do unit testing?

Question of the same name: http://www.zhihu.com/question/28278690

First, think about whether you want to do a unit test or a functional test.
If your goal is to verify that both pages are working correctly, what you want to do is functional testing. In this case, the test data can be configured first and tested with tools such as selenium.
If you want to do unit testing, which unit do you want to test?
It may be very simple to look at a page that shows the ID and title from the code, but it's not a unit.
In fact, it handles the HTTP request, binds the URL parameter, queries the database with parameters, reads the required data from the database return result, does the business logic needs processing , binds some data to the page template according to the processing result, renders the template as an HTML page, Send the page to the client, if there is a script such as JS on the page, you also need to execute these scripts in the client browser. Then the user actually sees the page.
Any error on the previous step will cause the page to work abnormally.
Few program units can be independent of the environment to complete the function, it can do is based on the input provided by the environment, give the correct output. Accordingly, the Unit test does not guarantee that the whole function is working properly, only to verify that the unit accepts a specific input and gives the expected output.
Fortunately, most of the dependencies are mature third-party code. In general, as long as the correct input data is given, they produce the correct output (behavior). Thus we can assume that the focus of our testing is to make sure that the code we write ourselves depends on the correct output to others after getting the correct input from the dependent environment.
For example, when you test, you generally do not doubt the input? Id=1 your code reads the id=0 from the parameter, because that is the third party your code trusts, you just have to confirm that the input is correct. Similarly, if you confirm that the SQL statement for delete is correct, it is generally not necessary to suspect that the database is executing the statement correctly.
From your point of view, most of your questions focus on functional testing rather than the unit, so maybe unit testing is not what you really need. If it is to be done, it may be important to measure the part of the MD5 related.
Another question about unit testing that goes beyond testing is that your description seems to have no units in your program, which can also cause some difficulty in unit testing.

Run the tests with a separate database, empty the database before each test starts, and regenerate the test data.

Another approach is to log the ID of the generated data each time the test data is generated, and then delete the resulting data after the test is complete, rather than emptying the database every time. This scenario is better suited for situations where both automated testing and manual testing are required, and automated testing does not result in the deletion of data that is added when manual tests are taken.

Testing is best not to rely on specific values (such as your example's id=1), it is best to use a separate function to create each test data (such as generateAccount and generateArticle ), these functions return the ID of the data created, otherwise if the hard-coded ID, add or remove the test will be very cumbersome.

Use three database configurations:
Dev: Database configuration used at development time
Test: Database configuration for testing, each auto-emptying and load fixtures
Production: Database configuration during the production phase of the deployment

You can use the isolating test, mock, fakes keyword search to know how to do the external data/logic depends on the unit test.

The plugin has to initialize this directory in your git. The reason why he did not find the current path of the Git folder, this folder has a remote address.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.