How can I prepare data for TDD?

Source: Internet
Author: User

Preparing data before writing a test is a headache. For a test, I have at least two methods to prepare data:

1. Create a file with data and load it in the before Method

2. Create a builder inCodeTo build a file stream or memory object for data.

Both methods can achieve my goal, so what method should I use? First, I need to know the advantages and disadvantages of the two methods?

1. template data files. This method is fast and easy to prepare data. We can export a file from the actual system for testing, and there can be a large amount of data. However, the disadvantage is that the test code must know the location of the file, and the deployment of the test is troublesome, and changes in the file format will cause the test to fail. If different tests have different requirements on data content, you need to create a new file to adapt to the test changes.

2. dynamically create data streams/memory objects. This method is relatively flexible and allows you to organize data at will. The data creation process can be better synchronized with the test code, it is not affected by the location of the data file. The disadvantage is that it is very laborious and slow to prepare large data volumes.

So the conclusion is that when our tests require a small amount of data and flexible format, we should use dynamic data streams/memory objects, which are usually characteristic of unit tests. If our testing requires a large amount of data in a fixed format, we should use a template data file. This feature is usually characteristic of integration testing or functional testing.

The theory can produce results only when it comes to practice. The following describes this theory by creating a CVS read/write object.

Suppose I want to write a cvsoperator to read and write the CVS file, so that I can get the field content through the row number and column number. At this time, I was wondering where I was looking for a ready-made CVS file? Or create a memory object for the CVS builder. According to TDD, we should move forward with as few steps as possible, and a large and complete CVS file is too complicated for me. So I created a simple cvsfilebuilder object.

 
Class cvsfilebuilder {private stringbuffer = new stringbuffer (); Public bytearrayinputstream getcvsfilestream () {bytearrayinputstream cvsfilestream = new bytearrayinputstream (stringbuffer. tostring (). getbytes (); Return cvsfilestream;} public void buildhead (string line) {stringbuffer. append (LINE + "\ r \ n ");}}

It is very simple, that is, read and write streams. I got a test,

 @ Test  Public   Void Should_read_head_from_cvs_file () Throws Ioexception {cvsfilebuilder simplebuilder = New  Cvsfilebuilder (); simplebuilder. buildhead ( "ID, user_name, password" ); Cvsoperator = New  Cvsoperator (); cvsoperator.Load(Simplebuilder. getcvsfilestream ());Cvsoperator. cvsheadHead = Cvsoperator.Gethead(); Assertthat (head.Getfield( 0), is ("ID"); Assertthat (head.Getfield( 1), is ("user_name" ); Assertthat (head.Getfield( 2), is ("password" ));} 

Because cvsoperator does not have any implementation code at this time, you will see a lot of red warnings. This is also the implementation method. So I will continue coding.

 Public   Class  Cvsoperator {  Private  Cvshead head;  Public   Void Load (bytearrayinputstream cvsfilestream) Throws Ioexception {bufferedreader br = New Bufferedreader ( New  Inputstreamreader (cvsfilestream); Head = New  Cvshead (Br. Readline ());}  Public  Cvshead gethead (){  Return  Head ;}  Class  Cvshead {  Private   Final String [] headfields;  Public  Cvshead (string line) {headfields = Line. Split ("," );}  Public String getfield ( Int  Index ){  Return (Headfields. length> index )? Headfields [Index]: Null  ;}}} 

Making tests pass quickly is a principle of TDD. Not complete to be continued ^_^

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.