<p>How are people unit testing their business applications? I've seen a lot of examples of unit testing with "simple to test" examples. Ex. a calculator. How are people unit testing data-heavy applications? How are you putting together your sample data? In many cases, data for one test may not work at all for another test which makes it hard to just have one test database?</p>

<p>Testing the data access portion of the code is fairly straightforward. It's testing out all the methods that work against the data that seem to be hard to test. For example, imagine a posting process where there is heavy data access to determine what is posted, numbers are adjusted, etc. There are a number of interim steps that occur (and need to be tested) along with tests afterwards that ensure the posting was successful. Some of those steps may actually be stored procedures.</p>

<p>In the past I've tried inserting the test data in a test database, then running the test, but honestly it's pretty painful to write this kind of code (and error prone). I've also tried just building a test database up front and rolling back the changes. That works OK but in a number of places you can't easily do this either (and many people would say that's integration testing; so be it, I still need to be able to test this somehow).</p>

<p>If the answer is that there isn't a nice way of handling this and it currently just sort of sucks, that would be useful to know as well.</p>

<p>Any thoughts, ideas, suggestions, or tips are appreciated.</p>

[{'answer_id': 38605, 'author': 'SCdF', 'author_id': 1666, 'author_profile': 'https://Stackoverflow.com/users/1666', 'pm_score': 0, 'selected': False, 'text': '<p>For lots of different runs over the same logic but with different data you can use CSV, as many columns as you like for the input and the last for the output etc.</p>\n'}, {'answer_id': 38628, 'author': 'Phil Bennett', 'author_id': 2995, 'author_profile': 'https://Stackoverflow.com/users/2995', 'pm_score': 2, 'selected': False, 'text': "<p>It depends on what you're testing.  If you're testing a business logic component -- then its immaterial where the data is coming from and you'd probably use a mock or a hand rolled stub class that simulates the data access routine the component would have called in the wild.  The only time I mess with the data access is when I'm actually testing the data access components themselves.  </p>\n\n<p>Even then I tend to open a DB transaction in the TestFixtureSetUp method (obviously this depends on what unit testing framework you might be using) and rollback the transaction at the end of the test suite TestFixtureTeardown.</p>\n"}, {'answer_id': 38631, 'author': 'David Hall', 'author_id': 2660, 'author_profile': 'https://Stackoverflow.com/users/2660', 'pm_score': 1, 'selected': False, 'text': '<p>It sounds like you might be testing message based systems, or systems with highly parameterised interfaces, where there are large numbers of permutations of input data.</p>\n\n<p>In general all the rules of standard unti testing still hold:  </p>\n\n<ul>\n<li>Try to make the units being tested as small and discrete as possible.</li>\n<li>Try to make tests independant.</li>\n<li>Factor code to decouple dependencies.</li>\n<li>Use mocks and stubs to replace dependencies (like dataaccess)</li>\n</ul>\n\n<p>Once this is done you will have removed a lot of the complexity from the tests, hopefully revealing good sets of unit tests, and simplifying the sample data.</p>\n\n<p>A good methodology for then compiling sample data for test that still require complex input data is <a href="http://en.wikipedia.org/wiki/Orthogonal_array" rel="nofollow noreferrer">Orthogonal testing</a>, or see <a href="http://www.isixsigma.com/library/content/c030106a.asp" rel="nofollow noreferrer">here</a>.</p>\n\n<p>I\'ve used that sort of method for generating test plans for WCF and BizTalk solutions where the permutations of input messages can create multiple possible execution paths.</p>\n'}, {'answer_id': 38632, 'author': 'Mark Harris', 'author_id': 4026, 'author_profile': 'https://Stackoverflow.com/users/4026', 'pm_score': 2, 'selected': False, 'text': '<p>Mocking Frameworks enable you to test your business objects. \nData Driven tests often end up becoming more of a intergration test than a unit test, they also carry with them the burden of managing the state of a data store pre and post execution of the test and the time taken in connecting and executing queries.</p>\n\n<p>In general i would avoid doing unit tests that touch the database from your business objects. As for Testing your database you need a different stratergy.</p>\n\n<p>That being said you can never totally get away from data driven testing only limiting the amout of tests that actually need to invoke your back end systems.</p>\n'}, {'answer_id': 38636, 'author': 'Toran Billups', 'author_id': 2701, 'author_profile': 'https://Stackoverflow.com/users/2701', 'pm_score': 3, 'selected': True, 'text': '<p>I have to second the comment by @Phil Bennett as I try to approach these integration tests with a rollback solution.</p>\n\n<p>I have a very detailed post about integration testing your data access layer <a href="https://stackoverflow.com/questions/12374/has-anyone-had-any-success-in-unit-testing-sql-stored-procedures#25204">here</a></p>\n\n<p>I show not only the sample data access class, base class, and sample DB transaction fixture class, but a full CRUD integration test w/ sample data shown.  With this approach you don\'t need multiple test databases as you can control the data going in with each test and after the test is complete the transactions are all rolledback so your DB is clean.</p>\n\n<p>About unit testing business logic inside your app, I would also second the comments by @Phil and @Mark because if you mock out all the dependencies your business object has, it becomes very simple to test your application logic one entity at a time ;)</p>\n\n<p>Edit:  So are you looking for one huge integration test that will verify everything from logic pre-data base / stored procedure run w/ logic and finally a verification on the way back?  If so you could break this out into 2 steps:</p>\n\n<ul>\n<li><p>1 - Unit test the logic that happens before the data is pushed\ninto your data access code.  For\nexample, if you have some code that\ncalculates some numbers based on\nsome properties -- write a test that\nonly checks to see if the logic for\nthis 1 function does what you asked\nit to do.  Mock out any dependancy\non the data access class so you can\nignore it for this test of the\napplication logic alone.</p></li>\n<li><p>2 - Integration test the logic that happens once you take your\n    manipulated data (from the previous\n    method we unit tested) and call the\n    appropriate stored procedure.  Do\n    this inside a data specific testing\n    class so you can rollback after it\'s\n    completed.  After your stored\n    procedure has run, do a query\n    against the database to get your\n    object now that we have done some\n    logic against the data and verify it\n    has the values you expected\n    (post-stored procedure logic /etc )</p></li>\n</ul>\n\n<p>If you need an entry in your database for the stored procedure to run, simply insert that data before you run the sproc that has your logic inside it.  For example, if you have a product that you need to test, it might require a supplier and category entry to insert so before you insert your product do a quick and dirty insert for a supplier and category so your product insert works as planned.</p>\n'}, {'answer_id': 38682, 'author': 'Jay Stramel', 'author_id': 3547, 'author_profile': 'https://Stackoverflow.com/users/3547', 'pm_score': 3, 'selected': False, 'text': '<p>My automated functional tests usually follow one of two patters:</p>\n\n<ul>\n<li>Database Connected Tests</li>\n<li>Mock Persistence Layer Tests</li>\n</ul>\n\n<p><strong>Database Connected Tests</strong></p>\n\n<p>When I have automated tests that are connected to the database, I usually make a single test database template that has enough data for all the tests.  When the automated tests are run, a new test database is generated from the template for every test.  The test database has to be constantly re-generated because test will often change the data.  As tests are added, I usually append more data to the test database template.</p>\n\n<p>There are some nice advantages to this testing method.  The obvious advantage is that the tests also exercise your schema.  Another advantage is that after setting up the initial tests, most new tests will be able to re-use the existing test data.  This makes it easy to add more tests.</p>\n\n<p>The downside is that the test database will become unwieldy.  Because data will usually be added one test at time, it will be inconsistent and maybe even unrealistic.  You will also end up cursing the person who setup the test database when there is a significant database schema change (which for me usually means I end up cursing myself).</p>\n\n<p>This style of testing obviously doesn\'t work if you can\'t generate new test databases at will.</p>\n\n<p><strong>Mock Persistence Layer Tests</strong></p>\n\n<p>For this pattern, you create <a href="http://www.mockobjects.com/" rel="nofollow noreferrer">mock objects</a> that live with the test cases.  These mock objects intercept the calls to the database so that you can programmatically provide the appropriate results.  Basically, when the code you\'re testing calls the <code>findCustomerByName()</code> method, your mock object is called instead of the persistence layer.</p>\n\n<p>The nice thing about using mock object tests is that you can get very specific.  Often times, there are execution paths that you simply can\'t reach in automated tests w/o mock objects.  They also free you from maintaining a large, monolithic set of test data.</p>\n\n<p>Another benefit is the lack of external dependencies.  Because the mock objects simulate the persistence layer, your tests are no longer dependent on the database.  This is often the deciding factor when choosing which pattern to choose.  Mock objects seem to get more traction when dealing with legacy database systems or databases with stringent licensing terms.</p>\n\n<p>The downside of mock objects is that they often result in a lot of extra test code.  This isn\'t horrible because almost any amount of testing code is cheap when amortized over the number of times you run the test, but it can be annoying to have more test code then production code.</p>\n'}]
['https://Stackoverflow.com/questions/38598', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3656/']