How does a data-dependent project Perform unit tests?

Source: Internet
Author: User
Unit tests are required for a project to ensure program robustness, so as to ensure normal functionality during version iterations. However, if a program (such as CMS) is dependent on the database itself, how should we conduct unit tests without compromising the database? Example: access? Id1. a project requires unit tests to ensure program robustness, so as to ensure normal functionality during version iterations. However, if a program (such as CMS) is dependent on the database itself, how should we conduct unit tests without compromising the database?

Example:
Access? Id = 1 page, first query

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

And assign it$data->ID,$data->TitleThese attributes;Make sure the values of these attributes are normal..
Then, based on some information, calculate$data->Hash, Such$data->Hash = md5($data->ID),The calculated values must also be normal..
Then, deleteid=1To

sqlDELETE FROM `data` WHERE `ID` = 1

,The row where the ID is located must be deleted.
Second, an article published in the background must make the database moreID=2, The content is the set value of the article. However, the content of HTML code may not be the same, and additional spaces and line breaks may be allowed.
You also need to post an article on simulating XML-RPC protocols.

As mentioned above, how should we conduct unit tests?

Know the same name problem: http://www.zhihu.com/question/28278690

Reply content:

Unit tests are required for a project to ensure program robustness, so as to ensure normal functionality during version iterations. However, if a program (such as CMS) is dependent on the database itself, how should we conduct unit tests without compromising the database?

Example:
Access? Id = 1 page, first query

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

And assign it$data->ID,$data->TitleThese attributes;Make sure the values of these attributes are normal..
Then, based on some information, calculate$data->Hash, Such$data->Hash = md5($data->ID),The calculated values must also be normal..
Then, deleteid=1To

sqlDELETE FROM `data` WHERE `ID` = 1

,The row where the ID is located must be deleted.
Second, an article published in the background must make the database moreID=2, The content is the set value of the article. However, the content of HTML code may not be the same, and additional spaces and line breaks may be allowed.
You also need to post an article on simulating XML-RPC protocols.

As mentioned above, how should we conduct unit tests?

Know the same name problem: http://www.zhihu.com/question/28278690

First, you must first understand what you want to doUnitTest or function test.
If you want to verify whether the two pages work properly, you want to perform a functional test. In this case, you can configure the test data and use tools such as selenium for testing.
Which unit test do you want to test?Unit?
From the code, it may be very simple to view a page with the ID and title, but it is not a unit.
In fact, it processes the http request, binds the URL parameter, queries the database with the parameter, reads the required data from the returned results of the database, and performsProcessing required by business logic, Bind some data to the page template according to the processing result, render the template as an html page, and send the page to the client. If there is a js script on the page, you also need to execute these scripts in the client browser. Then the user can see the page.
An error in any of the above steps will make the page abnormal.
Few program units can independently complete functions without relying on the environment. What it can do is to provide the correct output based on the input provided by the environment. Correspondingly, the unit test cannot ensure that the entire function works normally. It can only verify that the Unit receives 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 will produce the correct output (behavior ). Therefore, we can assume that the focus of our tests is to ensure that the code we write is output to other Dependencies after correct input is obtained from the dependency environment.
For example, when you test, you generally do not doubt the input? After id = 1, your code reads id = 0 from the parameter, because it is a third-party trusted by your code, you just need to confirm that the input to it is correct. Similarly, if you confirm that the delete SQL statement is correct, generally there is no need to doubt whether the database correctly executes this statement.
From your perspective, most of your questions are about functional testing rather than unit testing, so unit testing is not what you really need. If you want to do this, you may need to test the md5-related part.
Another question about unit testing but beyond testing, from your description, it seems that there are no units in your program, which may also cause some difficulties for unit testing.

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

Another way is to record the ID of the generated data each time the test data is generated, and then delete the generated data after the test is completed, instead of clearing the database every time. This solution is suitable for both automatic testing and manual testing. Automatic Testing will not delete the data added during manual testing.

It is best not to rely on specific values for testing (for example, ID = 1 in your example). It is best to use a separate function to create each test data (for examplegenerateAccountAndgenerateArticle), These functions return the ID of the created data. Otherwise, if the ID is hard-coded, it will be difficult to add or remove the test.

Use three database configurations:
Dev: database configuration used during development
Test: database configuration used for testing. Each time it is automatically cleared and loaded fixtures
Production: Database Configuration During production deployment

You can search for isolating test, mock, and fakes keywords to find out how to perform unit tests on external data/logic dependencies.

The plug-in must initialize this directory in your git. The reason is that the git folder in the current path is not found, and the folder contains 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.