Query analyzer development code test check_mysql

Source: Internet
Author: User
Develop code in the query analyzer to test and check if you are like me, you may have spent a lot of time developing code in the query analyzer. After you are satisfied with the code, you can immediately run one or two specialized tests on the test database on the development server. If it seems that there is no problem, you can put the code into production. If this is a key piece of code or the code is complex, you may perform more checks to avoid posterior profiling. Even in this case, you may be holding your screen. Www. chinai tp collection ow er. comd1nHRZf

This is the encoding method I have used in most of my career. Oh, sometimes I store test queries for future use, usually because the President/CEO/CIO/department manager is used to changing his/her requirements about every week. However, I will not do anything else. I usually use a dedicated query outside the query analyzer or its Oracle/Access/FoxPro equivalent tool for testing. The query analyzer debugger is required for higher-intensity testing. In desperate circumstances, PRINT statements are required. Www. chinai tp collection ow er. comd1nHRZf

There is currently a better way. Www. chinai tp collection ow er. comd1nHRZf

Test www. chinai tp acquisition ow er. comd1nHRZf

When my SIL Division adopted extreme programming (XP), we also adopted the unit testing section of this methodology, both of which made me a better developer. However, even if you are not working in an XP environment, you can still benefit from an XP unit test. Www. chinai tp collection ow er. comd1nHRZf

Unit tests are different from acceptance tests. Unit tests are used to test small code blocks (such as stored procedures), and acceptance tests are more related to whether users can accept the UI. The following are five advantages of the unit test I found: www. chinai tp collects ow er. comd1nHRZf

? They can identify the parties that should be held responsible. Have you ever received an email telling you that a program error should be fixed, which is actually a side effect of a change made by someone else? Well, if you have some scattered test queries, wrap them into a stored procedure that can run regularly (maybe at night. Make sure that an email is generated when the unit test fails. Www. chinai tp collection ow er. comd1nHRZf

? It does not take a long time to generate a database. Each stored procedure and each stored function should have a test written for it, and the trigger should also. If this sounds harsh, think about how nice it would be to capture the problem before it reaches the production server to save yourself. If you have a lot of old-style code, writing tests for each unit may take years, and you cannot stop new development simply to write tests. However, you can write a test for each new code segment, or write a test for each modified process. It won't take long for you to write many tests for key legacy code and new code. Www. chinai tp collection ow er. comd1nHRZf

? Easily create accurate code documents. Each process or function should be called using different combinations of parameters. This not only ensures that the code works as expected, but also provides the latest and accurate documentation on your work. Another coders only need to check your test to learn the example of calling your process. Who knows? One day, this other coder may be yourself. Www. chinai tp collection ow er. comd1nHRZf

? They force you to think and plan in advance. You should compile your unit test before writing the actual process or function. "What ?" You said, "I protest! How can we write and test for something that has not yet been encoded ?" Www. chinai tp collection ow er. comd1nHRZf

? There was a very old joke about it: a manager said, "I will figure out what they need. Other people start encoding ." Then, the coders cannot start working until they know what they need to write, isn't it? When you write a test, you are forced to consider what you want to do before you start writing the test. Www. chinai tp collection ow er. comd1nHRZf

? They can indeed save you time. Developers often complain that writing a test takes more time than writing a test. Sometimes. However, consider the following: I recently accepted a task, that is, modifying one of the most difficult stored procedures I have ever encountered. It is an old-style code, but I wrote the test first. It took me a few days to complete, partly because of the requirements imposed on the process. Practice has proved that this test is very important for all the reasons I have just listed, and it becomes very precious when I have to rewrite this process to improve performance. Www. chinai tp collection ow er. comd1nHRZf

? Unit tests show that there are a lot of errors in the re-writing process, and I can quickly find the root cause of each error, the time spent is only a fraction of the time when no unit test is used. Then, when I think the task has been completed, the fuzzy test fails. One of the variables in the main loop has a defect. If the code is published to the production environment in this status, this will be a program error that is hard to capture. In the end, I completed the task faster than using other methods. Www. chinai tp collection ow er. comd1nHRZf

How to write T-SQL unit test www. chinai tp collection ow er. comd1nHRZf

Before I tell you about the T-SQL testing framework, first remind you to pay attention to two very basic principles: www. chinai tp collection ow er. comd1nHRZf

? First, you need a database with good test data. I use "good data" to represent real data from the real world. No matter how good a programmer you are, you cannot fully copy data for your applications. Even if the old system to be replaced is composed of paper, you need to find a data recorder to input data in some tables. Complete the work required to obtain real data. [Even so, there is still a test data generator. Please refer to the prompt "generate test data" in this issue-editor] www. chinai tp collects ow er. comd1nHRZf

? Second, it is not necessary to develop production databases. You should have a development or test database to meet your own needs. In the past, when I was developing a database in Oracle, I spent a week putting the development database on an outdated server. SQL Server developers have no such excuse. Www. chinai tp collection ow er. comd1nHRZf

After providing good data for the development database, you need a framework to run the test. You can write your own framework, but why? There is an available framework. Www. chinai tp collection ow er. comd1nHRZf

TSQLUnit introduction www. chinai tp collection ow er. comd1nHRZf

TSQLUnit is an open source unit testing framework for the T-SQL, written by Henrik Ekelund and available from the http://sourceforge.net/projects/tsqlunit. The following is an example of how to use it. Www. chinai tp collection ow er. comd1nHRZf

My TSQLUnit test adopts a similar three-part mode: 1) unit test settings, 2) execute the target process, and 3) check results. Www. chinai tp collection ow er. comd1nHRZf

During unit test setup, I often check to ensure that no one breaks my data while I am not paying attention: www. chinai tp collects ow er. comd1nHRZf

DECLARE @ nId INT, @ nNewId INT ?" -@ NNewId is for later

SELECT @ nId = [ID] FROM MyTable

WHERE MyField = 'whatever'

IF @ nId is null -- or @ ROWCOUNT = 0

EXEC tsu_failure 'The data has changed.

& Apos; whatever & apos; couldn & apos; t be found & apos'

IF block is used to check the expected records. If this record cannot be found, the test fails and an error message is generated. The test framework is moved to the next unit test. You do not need to use the unit test name in the failed message string, because when the test fails, TSQLUnit will name it for you. Www. chinai tp collection ow er. comd1nHRZf

Now, I call the stored procedure to be compiled: www. chinai tp collects ow er. comd1nHRZf

EXEC CreateMyTableNewRec @ nId, @ nNewId OUTPUT

As you can see, I have determined the output parameters from this new process. During the check result, I make sure that the output parameter is indeed filled with some content: www. chinai tp collects ow er. comd1nHRZf

IF @ nNewId IS NULL

EXEC tsu_failure

'A new record was not created for table MyTable .'

I can further check this value to see if the new record is created as expected. Www. chinai tp collection ow er. comd1nHRZf

Each TSQLUnit test is a stored procedure. Listing 1 shows how all the above code segments are combined: www. chinai tp collects ow er. comd1nHRZf

Listing 1. complete unit test for the T-SQL. Www. chinai tp collection ow er. comd1nHRZf

Create procedure ut_MyTable_NewRec

AS

-- = Setup = --

DECLARE @ nID INT, @ nNewId INT

SELECT @ nId = id from MyTable

WHERE MyField = 'whatever'

IF @ nId is null -- or @ ROWCOUNT = 0

EXEC tsu_failure 'The data has changed.

& Apos; Whatever & apos; couldn & apos; t be found & apos'

-- = Execute = --

EXEC CreateMyTableNewRec @ nId, @ nNewId OUTPUT

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.