Why Unit Testingl


Published on

This presentation will help developers to understand about unit testing and how to perform it.

  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Code refactoring is the process of changing a computer program 's source code without modifying its external functional behavior in order to improve some of the nonfunctional attributes of the software. Advantages include improved code readability and reduced complexity to improve the maintainability of the source code, as well as a more expressive internal architecture or object model to improve extensibility . Unit Testing is conducted by the Developer during code development process to ensure that proper functionality and code coverage have been achieved by each developer both during coding and in preparation for acceptance into iterations testing.
  • If you do all the stuff that you know you’re supposed to anyway (loose coupling, high cohesion, etc.), writing tests is really easy. :-) Seriously : Loosely coupled and highly cohesive software is much easier to test than tightly coupled systems, or ones where it’s hard to tell what something is really for (low cohesion). There’s a lot of Patterns for making that easier, like Inversion of Control, Strategies, etc. Writing the test before you write the code guarantees that your code is “testable.” With very few exceptions, code that is hard to test is a HUGE flag that the design is bad (i.e., tightly coupled, low cohesion). If you have dependencies between tests, or have to do tons of setup, then it’s a pain to write tests and to run them.
  • From past experience, projects go to lengths to separate tests from code but don't place a lot of emphasis on how to structure Test assemblies. Often, a single Test library is created, which is suitable for most projects. However, for large scale projects that can have hundreds of tests this approach can get difficult to manage. I'm not suggesting that you should religiously enforce test structure, but there may be logical motivators to divide your test assemblies into smaller units, such as grouping tests with third-party dependencies or as an alternative for using Categories. Again, separate when needed, and use your gut to tell you when you should.
  • 1. Eventually, you'll want to circle back on these tests and either fix them or alter them so that they can be used. But without an explaination, the next person will have to do a lot of investigative work to figure out that reason. In my experience, most tests with the Ignore attribute are never fixed.
  • Why Unit Testingl

    1. 1. Unit Testing Prepared by: Priya Sharma Trivedi
    2. 2. <ul><li>Introduction </li></ul><ul><li>The need of testing and it’s Terminology </li></ul><ul><li>Unit Testing </li></ul><ul><li>Right B.I.C.E.P.s.? </li></ul><ul><li>Benefits of Unit Testing </li></ul><ul><li>Naming standards for unit tests </li></ul>Agenda
    3. 3. Result of this problem <ul><li>error - a mistake made by a human (in a software development activity) </li></ul><ul><li>defect (or fault ) - the result of introducing an error into a software artifact (SRS, SDS, code, etc.) </li></ul><ul><li>failure - a departure from the required behavior for a system </li></ul>A vicious cycle – the more pressure a you (Developer) feel, the fewer tests you writes. The fewer tests you write, the less productive you are and the less stable your code becomes. The less productive and accurate you are, the more pressure you feel. The Problem
    4. 4. Unit Testing <ul><li>Unit testing is a software verification and validation method in which a programmer tests if individual units of source code are fit for use. </li></ul><ul><li>The goal of unit testing is to isolate each part of the program and show that the individual parts are correct. </li></ul><ul><li>Unit Testing checks that behaves correctly. </li></ul><ul><ul><li>Static Testing </li></ul></ul><ul><ul><ul><li>testing a unit without executing the unit code </li></ul></ul></ul><ul><ul><li>Dynamic Testing </li></ul></ul><ul><ul><ul><li>testing a unit by executing a program unit using test data </li></ul></ul></ul><ul><li>Some common definitions of a unit: </li></ul><ul><ul><li>A unit is the smallest testable part of an application. </li></ul></ul><ul><ul><li>In procedural programming a unit may be an individual function or procedure. </li></ul></ul><ul><ul><li>The smallest chunk that can be compiled by itself </li></ul></ul><ul><ul><li>A stand alone procedure of function </li></ul></ul><ul><ul><li>Something so small that it would be developed by a single person </li></ul></ul>
    5. 5. <ul><li>Without unit tests defects show up in the end of development </li></ul><ul><li>Finding them is difficult. Removing them time-consuming and Complex </li></ul><ul><li>With unit tests we can find defects much sooner and easier, continuously removing them before adding new features </li></ul><ul><li>We can also check if new features break our code </li></ul>Why Unit Testing????????????
    6. 6. Unit Test: <ul><li>Unit-tests </li></ul><ul><li>Should validate expected functionality </li></ul><ul><li>at the level of: </li></ul><ul><li>individual class </li></ul><ul><li>small groups of collaborating classes </li></ul><ul><li>work package </li></ul><ul><li>Test execution framework </li></ul><ul><li>Testing naming and structure </li></ul><ul><li>Test case specification template </li></ul><ul><li>Important code should have unit tests </li></ul><ul><li>Tests should be written together with the code </li></ul><ul><li>Code should pass minimum 70% of the unit tests before it can be released </li></ul>Software developer CODE Test programs Automatic software test run process Unit-test Component Code Documentation
    7. 7. Unit-test: Test frameworks <ul><li>Our constrains: </li></ul><ul><li>TIME </li></ul><ul><li>What we are doing: </li></ul><ul><li>Trying Unit Testing Tools </li></ul><ul><li>in the our projects. </li></ul><ul><li>Preparing “HowTo” documents to make easier the installation of these tools and the start with process </li></ul><ul><li>Our plans: </li></ul><ul><li>Implementation of the Unit Testing </li></ul><ul><li>Procedure in the our project environment. </li></ul><ul><li>to produce code for unit-testing </li></ul><ul><li>to run tests in automatic way </li></ul>What we propose: JUnit NUnit SimpleTest Aim: to help developers: <ul><li>Our inputs: </li></ul><ul><li>What is available as free open source code. </li></ul>
    8. 8. Unit Testing makes your developer lives easier <ul><li>Easier to find bugs </li></ul><ul><li>Easier to maintain </li></ul><ul><li>Easier to understand </li></ul><ul><li>Easier to Develop </li></ul>You have already done Unit testing <ul><li>Not structured </li></ul><ul><li>Not Repeatable </li></ul><ul><li>Not on all your code </li></ul><ul><li>Not easy to do as it should be </li></ul><ul><li>A framework is missing </li></ul>
    9. 9. Designing unit tests Type of Unit Test Positive Test Negatives Test the function with valid input data Test the function With invalid data
    10. 10. When to write the test <ul><li>During Development - When you need to add new functionality to the system, write the tests first. Then you will be done developing when the test runs. </li></ul><ul><li>During Debugging - When someone discovers a defect in your code, first write a test that will succeed if the code is working. Then debug until the test succeeds. </li></ul>“ Whenever you are tempted to type something into a print statement or a debugger expression, write it as a test instead.”...
    11. 11. What should be tested ? (subprogram, object class, package, module) <ul><ul><li>An individual program unit </li></ul></ul>Test for both success & failure. For boundary conditions For general functionality Etc.. <ul><ul><ul><li>Databases, Stored Procedures, Triggers, Tables, Indexes </li></ul></ul></ul><ul><ul><ul><li>NT Services </li></ul></ul></ul><ul><ul><ul><li>Database conversion </li></ul></ul></ul><ul><ul><ul><li>.OCX, .DLL, .EXE and other binary formatted executables </li></ul></ul></ul><ul><ul><li>Invariants: things that must always be true. </li></ul></ul><ul><ul><li>Post-conditions: things that must be true after you run the code. </li></ul></ul><ul><ul><li>Pre-conditions: Those things that must be true before you run the code. </li></ul></ul><ul><ul><li>(Eg. The list size will always be greater than or equal to 0) </li></ul></ul><ul><ul><li>(Eg. The object’s buffer has been initialized) </li></ul></ul><ul><ul><li>(Eg. method will never return null) </li></ul></ul>
    12. 12. Example of How To Think of Tests <ul><li>The required functionality for a particular service (getVersionsForProductId) is the ability to get a list of all versions for a given product id. If there’s no product for that id, then an exception is thrown. If there’s no versions for a valid product id, then an empty list is returned. </li></ul>The Tests That Are Needed <ul><ul><li>Starting from the easiest to the hardest: </li></ul></ul><ul><ul><li>If there’s no product for that id, then an exception is thrown (pre-condition) </li></ul></ul><ul><ul><li>If there’s no versions for a valid product id, then an empty list is returned (post-condition) </li></ul></ul><ul><ul><li>If there are versions for a product id, then a non-empty list of all the versions is returned (post-condition) </li></ul></ul>
    13. 13. Unit Testing Tasks and Steps: <ul><li>Step 1: Create a Test Scenario </li></ul><ul><li>Step 2: Create Test Cases and Test Data </li></ul><ul><li>Step 3: If applicable create scripts to run test cases </li></ul><ul><li>Step 4: Once the code is ready execute the test cases </li></ul><ul><li>Step 5: Fix the bugs if any and re test the code </li></ul><ul><li>Step 6: Repeat the test cycle until the “unit” is free of all bugs </li></ul>
    14. 14. Testing Steps
    15. 15. What Makes a Good Unit Test? <ul><li>It sufficiently tests the contract. It doesn’t need to be complete, just “sufficient.” </li></ul><ul><ul><li>If it’s complex code, then have lots of tests. </li></ul></ul><ul><ul><li>If it’s a simple accessor, then you don’t need a test. </li></ul></ul><ul><ul><li>Most things are in between… </li></ul></ul><ul><li>It runs quickly. Part of the point is to make it so that you’re able to run them regularly, so speed plays into that. </li></ul><ul><ul><li>It’s much easier to fix a bug you introduced five minutes ago than one you did five weeks ago… </li></ul></ul><ul><li>Tests are independent. </li></ul><ul><ul><li>Loosely coupled functionality enables independent tests. </li></ul></ul><ul><ul><li>Write loosely coupled, highly cohesive code! </li></ul></ul><ul><li>Run the tests regularly. </li></ul><ul><ul><li>You can be “fearless” when you need to make changes, because you don’t have to worry that you might’ve broken something. </li></ul></ul>
    16. 16. <ul><li>Is incomplete (e.g. missing dependencies) </li></ul><ul><li>Doesn’t compile </li></ul><ul><li>Compiles but breaks other code </li></ul><ul><li>Doesn’t have unit tests </li></ul><ul><li>Has failing unit tests </li></ul><ul><li>Passes its tests but causes other tests to fail. </li></ul>Do not check in code that… When to Run Tests <ul><li>When you write a new method… … compile and run local unit tests </li></ul><ul><li>When you fix a bug… … run the test that illustrates that bug. </li></ul><ul><li>Any successful compile… … run local unit tests. </li></ul><ul><li>Before you check in… … run all tests. </li></ul><ul><li>Continuously… ... check out and build the project from scratch including all unit tests. </li></ul>
    17. 17. Test Case Sample <ul><li>Additionally the following information may also be captured: a) Unit Name and Version Being tested b) Tested By c) Date d) Test Iteration (One or more iterations of unit testing may be performed) </li></ul>Comment if any Pass / Fail What actually happens. This column can be omitted when defect recording tool is used. What should happen? Input Data How to Test What to Test ID which can be referred to in other docs like “TM” “Root Cause Analysis of defects etc Remarks Pass / Fail Actual Result Expected Result Input Data TC Procedure Test Case Purpose Test Case
    18. 18. Steps to Effective Unit Testing <ul><li>Documentation: Early on document all the Test Cases needed to test your code. Document the Test Cases, actual Results when executing the Test Cases, Response Time of the code for each test case. </li></ul><ul><li>Important advantages if the test cases and the actual execution of test cases are well documented. </li></ul><ul><ul><ul><li>Documenting Test Cases prevents oversight . </li></ul></ul></ul><ul><ul><ul><li>Documentation clearly indicates the quality of test cases . </li></ul></ul></ul><ul><ul><ul><li>If the code needs to be retested we can be sure that we did not miss anything. </li></ul></ul></ul><ul><ul><ul><li>It provides a level of transparency of what was really tested during unit testing. This is one of the most important aspects. </li></ul></ul></ul><ul><ul><ul><li>It helps in knowledge transfer in case of employee attrition. </li></ul></ul></ul><ul><ul><ul><li>Sometimes Unit Test Cases can be used to develop test cases for other levels of testing </li></ul></ul></ul>
    19. 19. What should be tested when Unit Testing It could be a screen or a component or a web service <ul><ul><li>Test Cases to verify all the screen elements that need to appear on the screens. </li></ul></ul><ul><ul><li>Test cases to verify the spelling/font/size of all the “labels” or text that appears on the screen. </li></ul></ul><ul><ul><li>Test Cases such that every line of code in the unit is tested at least once in a test cycle. </li></ul></ul><ul><ul><li>Test Cases such that every condition in case of “conditional statements” is tested once. </li></ul></ul><ul><ul><li>Test Cases to test the minimum/maximum range of data that can be entered. </li></ul></ul><ul><ul><li>Test Cases to verify how various errors are handled . </li></ul></ul><ul><ul><li>Test Cases to verify if all the validations are being performed </li></ul></ul>
    20. 20. Never write a test that succeeds the 1 st time Start with null case, or something that doesn’t work Try something trivial to make the test work Loose coupling & testability go hand in hand Use mock Objects Write the test first Charles' Six Rules of Unit Testing
    21. 21. <ul><li>Keep unit tests small and fast </li></ul><ul><li>Unit tests should be fully automated and non-interactive </li></ul><ul><li>Make unit tests simple to run </li></ul><ul><li>Measure the tests </li></ul><ul><li>Fix failing tests immediately </li></ul><ul><li>Keep testing at unit level </li></ul><ul><li>Start off simple </li></ul><ul><li>Keep tests independent </li></ul><ul><li>Keep tests close to the class being tested </li></ul><ul><li>Name tests properly </li></ul><ul><li>Test public API </li></ul><ul><li>Think black-box </li></ul><ul><li>Think white-box </li></ul><ul><li>Test the trivial cases too </li></ul><ul><li>Focus on execution coverage first </li></ul><ul><li>Cover boundary cases </li></ul><ul><li>Provide a random generator </li></ul><ul><li>Test each feature once </li></ul><ul><li>Use explicit asserts </li></ul><ul><li>Provide negative tests </li></ul><ul><li>Design code with testing in mind </li></ul><ul><li>Don't connect to predefined external resources </li></ul><ul><li>Write tests to reproduce bugs </li></ul><ul><li>Know the limitations </li></ul>Unit Testing Guidelines
    22. 22. Are we testing the Right B.I.C.E.P.s.? Testing without a strategy is futile. <ul><ul><li>Always go for the whole set . </li></ul></ul>s <ul><ul><li>Are Performance conditions within bounds? </li></ul></ul>P <ul><ul><li>Can you force Error conditions to happen? </li></ul></ul>E <ul><ul><li>Can you Cross-check using other methods/means? </li></ul></ul>C <ul><ul><li>Can you test the Inverse relationship? </li></ul></ul>I <ul><ul><li>Are all the Boundary conditions correct? </li></ul></ul>B <ul><ul><li>Are the results right ? </li></ul></ul>R
    23. 23. Right -BICEP <ul><li>Right : Are the results right ? </li></ul><ul><ul><li>Validate results </li></ul></ul><ul><ul><li>Does the expected result match what the method does? </li></ul></ul><ul><li>If you don’t know what “right” would be, then how can you test? How do you know if your code works? </li></ul><ul><ul><li>Perhaps requirements not known or stable </li></ul></ul><ul><ul><li>Make a decision. Your tests document what you decided. Reassess if it changes later. </li></ul></ul>
    24. 24. Right- B ICEP <ul><li>Boundary Conditions </li></ul><ul><ul><li>Garbage input values </li></ul></ul><ul><ul><li>Badly formatted data like ’1970-23-12’ when 1970-12-23 is required. </li></ul></ul><ul><ul><li>Empty or missing values (0, null, etc.) </li></ul></ul><ul><ul><li>Values out of reasonable range </li></ul></ul><ul><ul><li>Duplicates if they’re not allowed </li></ul></ul><ul><ul><li>Unexpected orderings </li></ul></ul><ul><ul><li>Totally bogus or inconsistent values, such as filenames. </li></ul></ul><ul><ul><li>Validity like age in years between 0 and 90 e.g. 115. </li></ul></ul><ul><ul><li>Things that arrive out of order. </li></ul></ul>
    25. 25. Right-B I CEP <ul><li>Check Inverse Relationships </li></ul><ul><ul><li>If your method does something that has an inverse, then apply the inverse </li></ul></ul><ul><ul><ul><li>E.g. square and square-root. Insertion then deletion. </li></ul></ul></ul><ul><ul><li>Beware errors that are common to both your operations </li></ul></ul><ul><ul><ul><li>Seek alternative means of applying inverse if possible </li></ul></ul></ul>
    26. 26. Right-BI C EP <ul><li>Are the boundaries C.O.R.R.E.C.T.? </li></ul><ul><li>C Conformance . Is the format Ok? </li></ul><ul><li>O Ordering. Is the set (un)ordered as appropriate e.g. as dictated by invariant. </li></ul><ul><li>R Is the value within acceptable Range ? </li></ul><ul><li>R Does the code Reference anything external not under its direct control? </li></ul><ul><li>E Does the value Exist ? </li></ul><ul><li>C C ountability; Are there enough values? </li></ul><ul><li>T Tim(e|ing). Is everything happening in the right order? </li></ul>
    27. 27. Right-BI C EP <ul><li>Cross-check using other means </li></ul><ul><li>Can you do something more than one way? </li></ul><ul><ul><li>Your way, and then the other way. Match? </li></ul></ul><ul><li>Are there overall consistency factors you can check? </li></ul><ul><ul><li>Overall agreement </li></ul></ul><ul><ul><li>If e.g. you are devising a square root for an embedded system without float capability, you could test it (in the host environment) using the standard implementation, like so: </li></ul></ul><ul><ul><li>public void testSquarerootUsingStd( ) { </li></ul></ul><ul><ul><li>double number=3830900.0; </li></ul></ul><ul><ul><li>double root1= mySquareRoot ( number ) ; </li></ul></ul><ul><ul><li>double root2= Math.sqrt ( number ) ; </li></ul></ul><ul><ul><li>assertEquals ( root2 , root1 , 0.0001 ) ; </li></ul></ul><ul><ul><li>} </li></ul></ul>
    28. 28. Right-BIC E P <ul><li>Force Error Conditions; Can your module survive? </li></ul><ul><li>Strange things in time keeping (like a clock that has been set back or summer and winter time). </li></ul><ul><li>Failures outside your code: </li></ul><ul><ul><li>Invalid parameters, out of range values, etc. </li></ul></ul><ul><ul><li>Out of memory, disk full, network down, etc. </li></ul></ul><ul><ul><li>Can simulate such failures </li></ul></ul><ul><ul><li>High system load. </li></ul></ul><ul><ul><li>Limited color availability in user interface. </li></ul></ul><ul><ul><li>Very high or very low screen resolution. </li></ul></ul>
    29. 29. Right-BICE P <ul><li>Performance </li></ul><ul><li>A normal unit test should test for the functional requirements . But if the non-functional requirement performance or speeds depend on the size of the data, you should test that too. </li></ul><ul><ul><li>Perhaps absolute performance, or </li></ul></ul><ul><ul><li>Perhaps how performance changes as input grows. </li></ul></ul><ul><ul><li>Perhaps separate test suite in JUnit </li></ul></ul><ul><li>Example: a spam filter that tests against “naughty” or black-listed mail servers. Test your code with different lists sizes. Typically a small list for build testing, but maybe also with a longer list e.g. once a day. </li></ul>
    30. 30. Benefits of Unit Testing It provides a strict, written contract that the piece of code must satisfy. It find problems early in the development cycle. It allows the programmer to refactor code at a later date , and make sure the module still works correctly (i.e. regression testing). It may reduce uncertainty in the units themselves and can be used in a bottom-up testing style approach. It provides a sort of living documentation of the system . Developers looking to learn what functionality is provided by a unit and how to use it. By testing the parts of a program first and then testing the sum of its parts, integration testing becomes much easier.
    31. 31. Result <ul><li># of Test Cases (Test Development Phase) </li></ul><ul><li># of Test Cases Executed (Test Execution Phase) </li></ul><ul><li># of Test Cases Passed (Test Execution Phase) </li></ul><ul><li># of Test Cases Failed (Test Execution Phase) </li></ul><ul><li># of Test Cases Under Investigation (Test Development Phase) </li></ul><ul><li># of Test Cases Blocked (Test dev/execution Phase) </li></ul><ul><li># of Test Cases Re-executed (Regression Phase) </li></ul><ul><li># of First Run Failures (Test Execution Phase) </li></ul><ul><li>Total Executions (Test Reporting Phase) </li></ul><ul><li>Total Passes (Test Reporting Phase) </li></ul><ul><li>Total Failures (Test Reporting Phase) </li></ul><ul><li>Test Case Execution Time ( (Test Reporting Phase) </li></ul><ul><li>Test Execution Time (Test Reporting Phase) </li></ul>
    32. 32. Result…Cont…… <ul><li>% Complete </li></ul><ul><li>% Defects Corrected </li></ul><ul><li>% Test Coverage </li></ul><ul><li>% Rework </li></ul><ul><li>% Test Cases Passed </li></ul><ul><li>% Test Effectiveness </li></ul><ul><li>% Test Cases Blocked </li></ul><ul><li>% Test Efficiency </li></ul><ul><li>1st Run Fail Rate </li></ul><ul><li>Defect Discovery Rate </li></ul><ul><li>Overall Fail Rate </li></ul>
    33. 33. Naming standards for unit tests BAD_DATA or EMPTY_ARRAY or NON_INITIALIZED_PERSON Variable names should express the expected input and state Public void Sum_NegativeNumAs1stParam_ExcepThrown() Test name should include name of tested method or class. [MethodName_StateUnderTest_ExpectedBehavior] testCalculator() Test Name should only begin with Test if it is required by the testing framework or if it eases development and maintenance of the unit tests in some way. Public void SumNegativeNumber2() Test name should be presented as a statement or fact of life that expresses workflows and outputs Public int Sum(params int[] values), Public int Sum_NumberIsIgnored() Test name should include the expected input or state and the expected result for that input or state Public void Sum_simpleValues_Calculated () Test name should express a specific requirement. The basic naming of a test comprises of three main parts [MethodName_StateUnderTest_ExpectedBehavior]
    34. 34. Naming standards for unit tests- Cont…… : The [Category] attribute when applied to a method associates the Test within a user-defined category. Category Tests with the [Ignore] attribute are skipped over when the Tests are run . Ignore: Tests with the [Explicit] attribute won't run unless you manually run them . Explicit: Similar to finalizers Fixture TearDown Similar to constructors. Fixture Setup a method with the [TearDown] attribute is called at the end of every test within a fixture. TearDown Test Fixtures can designate a special piece of code to run before every Test within that Fixture. That method is decorated with the [Setup] attribute. SetUP Methods within the Fixture that are decorated with the [Test] attribute and contain code that validates the functionality of our target . Test Test Suites are an older style of organizing tests. They're specialized fixtures that programmatically define which Fixtures or Tests to run. Suite Synonymous with &quot; TestFixture &quot;, a fixture is a class that contains a set of related tests. Fixture to refer to the piece of functionality that is testing. Target / Subject
    35. 35. Naming standards for unit tests- Cont…… you should only write the methods that you need today. Adding methods for future purposes only adds visual noise for maintenance purposes AVOID: Empty Setup methods (You can always go back) CONSIDER: Splitting Test Libraries into Multiple Assemblies Suites represent significant developer overhead and maintenance. Categories offer a unique advantage in the UI and at the command-line that allows you to specify which categories should be included or excluded from execution. CONSIDER: Using Categories instead of Suites or Specialized Tests For example, you could execute only &quot;Stateful&quot; tests against an environment to validate a database deployment. In scenarios where you are testing sets of common classes or when tests share a great deal of duplication, consider creating a base TestFixture that your Fixtures can inherit. CONSIDER: Deriving common Fixtures from a base Fixture If you have a requirement where you want to test in production or verify at the client's side, you can accomplish this simply by bundling the test library with your release. CONSIDER: Separating your Tests from your Production Code.
    36. 36. Naming standards for unit tests- Cont…… If your application has features that differ slightly for application roles, it's likely that your test names will overlap. CONSIDER: Using prefixes for Different Scenarios Some have adopted a For<Scenario> syntax (CanGetPreferencesForAnonymousUser). Other have adopted an underscore prefix _<Scenario> (AnonymousUser_CanGetPreferences). Since Exceptions are typically thrown when your application is a performing something it wasn't designed to do, prefix &quot;Cannot&quot; to tests that are decorated with the [ExpectedException] attribute. CONSIDER: Use &quot;Cannot&quot; Prefix for Expected Exceptions Examples: CannotAcceptNullArguments , CannotRetrieveInvalidRecord . Most tests require special knowledge about the functionality your testing, so a little documentation to explain what the test is doing is helpful. DO: Document your Tests A few comments here and there are often just the right amount to help the next person understand what you need to test and how your test approaches demonstrates that functionality. The test name should match a specific unit of functionality for the target type being tested. Some key questions you may want to ask yourself: &quot;what is the responsibility of this class?&quot; &quot;What does this class need to do?&quot; Think in terms of action words. DO: Name Tests after Functionality For example, a test with the name CanDetermineAuthenticatedState provides more direction about how authentication states are examined than Login .
    37. 37. Naming standards for unit tests- Cont…… Sometimes we create tests for bugs that are caught late in the development cycle, or tests to demonstrate requirements based on lengthy requirements documentation . As these are usually pretty important tests (especially for bugs that creep back in). AVOID: Unclear Test Names it's important to avoid giving them vague test names that represent a some external requirement like FixForBug133 or TestCase21 . PascalCase should suffice. Imagine all the time you save not holding down the shift key. AVOID: Using underscores as word-separators use_underscores_as_word_separators_for_readability, If you find that your tests are named after the methods within your classes, that's a code smell that you're testing your implementation instead of your functionality. AVOID: Naming Tests after Implementation If you changed your method name, would the test name still make sense? Tests that are marked with the Ignore attribute should include a reason for why this test has been disabled. AVOID: Ignore Attributes with no explanation
    38. 38. Naming standards for unit tests- Cont…… Finished with Unit Testing As Categories are sensitive to case and spelling, you might want to consider creating your own Category attributes by deriving from CategoryAttribute. CONSIDER: Defining Custom Category Attributes Using Categories is a powerful way to dynamically separate your tests at runtime, however their effectiveness is diminished when developers are unsure which Category to use. Categories DO: Limit the number of Categories
    39. 39. Summary <ul><li>“ Unit Testing” is the most important one. Detecting and fixing bugs early on in the Software Lifecycle helps reduce costly fixes later on. The Above article explains how Unit Testing should be done and the important points that should be considered when doing Unit Testing. </li></ul>