title: Unit Test vs Integration Test in Web Development
body: <p>I would like to ask about using Unit Testing in Web Development. The idea of Unit Testing is great but does it really bring value in the context of web application? Second part of my question is about TDD. If one creates an integration test before the actual code can this approach be called "Test Driven Development"? </p>
  
  <p><strong>1. Assumptions</strong></p>
  
  <p>By the definition Unit Test should test code on only one service layer. If a test tests code across multiple layers we have an integration test. </p>
  
  <p><strong>2. Argument</strong></p>
  
  <p><strong>2.1 No Algorithms</strong></p>
  
  <p>There are not many algorithms in a Web Application. It's not like building a 3D Physics engine where every method does something challenging and hard to debug. Web application is mostly about integration and generating HTML. </p>
  
  <p>The biggest challenges for a web application are: 
  - clean code (universal problem for any software but not testable)
  - data consistency
  - integration (Programming Language, File System, Configuration Files, Web Server, Caching Systems, Database, Search Engine, External APIs - all those systems have to work together at a request)</p>
  
  <p>If you want to build a Unit Test for every class in a web app you will be testing (in most cases): filling arrays, string concatenations and languages' native functions.</p>
  
  <p><strong>2.2 Cost</strong></p>
  
  <p>Just because a Web App is all about integration there are always multiple dependencies. You have to mock many different classes and writing even a small test might be actually a big job. What's even worse it's not only about test. Software needs to be testable. It means that one has to be able to inject dependencies in almost every class. It's not always possible to inject dependencies without creating additional layer between two classes (or systems). It complicates code and makes it more expensive to work with.</p>
  
  <p><strong>3. Integration Test</strong></p>
  
  <p>If web development is all about integration why not to test it? There are few counter arguments.</p>
  
  <p><strong>3.1 Integration test says "something is broken" but doesn't say where</strong></p>
  
  <p>That really comes down to: How much time does it take to find a bug when an integration test fails in comparison to time required for making code "UnitTestable" and more complicated (I guess this is subjective)? In my experience it never took long to find source of a problem.</p>
  
  <p><strong>3.2 You can run unit test on any environment, it's hard to do with an integration test</strong></p>
  
  <p>Yes, if you want to run integration test without a database. Usually there is a database. So long you operate on fix data and clean after each test then it should be fine. Transactional databases are perfect for this task. Open transaction, insert data, test, rollback.</p>
  
  <p><strong>3.3 Integration Tests are hard to maintain</strong></p>
  
  <p>I can't comment on that because all my test work well and I never had problem with that. </p>
  
  <p><strong>4. Create good Unit Tests!</strong></p>
  
  <p>The whole argument can be attacked with "If you create your Unit Test right, then you don't have any problems.". Can't that be the same with integration tests? If it's easier to create an integration test why not to stick with it and just make it right?</p>
  
  <p>Don't get me wrong I'm not against Unit Test. It's perfect idea and I recommend to everybody. I'm trying to understand: does it really fit web development? I would like to hear you opinions and experience. </p>