Se ha denunciado esta presentación.
Utilizamos tu perfil de LinkedIn y tus datos de actividad para personalizar los anuncios y mostrarte publicidad más relevante. Puedes cambiar tus preferencias de publicidad en cualquier momento.

.Net Unit Testing with Visual Studio 2010

20.089 visualizaciones

Publicado el

Integrate the Microsoft Unit Testing Framework into your Visual Studio 2010 projects. Learn the fundamentals of the Microsoft.VisualStudio.TestTools.UnitTesting namespace. Explore the tools for running and debugging your unit tests. Discuss general best practices for writing unit tests. Examples will be presented in C#.

Publicado en: Tecnología, Educación
  • Inicia sesión para ver los comentarios

.Net Unit Testing with Visual Studio 2010

  1. 1. Using Visual Studio 2010 By Karen Gayda
  2. 2.  The test project is created by selecting “New Project” from the File Menu. Expand Visual C# from the tree view on the left-hand side of the dialog. Select “Test” from the list of options. In the center pane, select “Test Project”. Give the project a name and add it to your application’s solution.
  3. 3.  Contains special classes designated by the [TestClass] attribute which are intended to run groups of tests. Generally speaking, each class in the application project will have a corresponding class in the unit test project. Some classes, such as UI classes, may not be tested by the Unit Test class for UI interactivity. This is tested with a macro-style test class type called a Coded UI Test. Non-UI methods/properties of a UI class can still be tested with a standard Unit Test class.
  4. 4.  Must have at least one test class in project. Project may have test data files for use by the unit tests. Project may have utility/helper classes for common functionality used by test classes and test methods. Project may use configuration files to setup state or provide environment information to the tests.
  5. 5.  Microsoft.VisualStudio.TestTools.UnitTesting namespace:  The namespace has many classes to help you with your unit tests including:  Assert classes that you can use to verify conditions in unit tests  Initialization and cleanup attributes to run code before or after unit tests run to ensure a specific beginning and ending state  The ExpectedException attribute to verify that a certain type of exception is thrown during unit test execution  The TestContext class which stores information that is provided to unit tests such as data connection for data- driven tests and information that is required to run unit tests for ASP.NET Web Services
  6. 6.  TestClass attribute:  When you create a unit test, the TestClass attribute is included in the test file to indicate that this particular class may contain methods marked with the [TestMethod()] attribute. Without the TestClass attribute, the test methods are ignored.  A test class can inherit methods from another test class that is in the same assembly. This means that you can create test methods in a base test class and then use those methods in derived test classes.
  7. 7.  TestContext - When you create unit tests, a class private variable called testContextInstance is included for each test class. It is of type TestContext. The properties of the TestContext class store information about the currently running test class.
  8. 8.  ClassInitialize method  Designated by the [ClassInitialize()] attribute.  Used to setup anything needed by all the test methods that will be run in the class. Examples of this would be to create test databases, to create tables, to copy test files, etc.  Takes TestContext variable as parameter.  One class initialization method per unit test class.
  9. 9.  ClassCleanUp method  Designated by the [ClassCleanUp()] attribute.  Used to cleanup artifacts of the test class for the test run.  Usually used to delete data, files, tables or databases created by the class.  One class cleanup method per test class.
  10. 10.  TestInitialize method  Designated by the [TestInitialize()] attribut.e  Method that runs before the execution of every test in the class.  Used to manage resources shared by the test methods in the class.  Used to setup test environment to an initialized/known state prior to a test executing.  One test initialize method per unit test class.
  11. 11.  TestCleanUp method  Designated by the [TestCleanUp()] attribute  Method runs after the execution of every test method in the unit test class.  Used to return the test environment to a known state or clean up the artifacts created by a test.  One test clean up method per unit test class.
  12. 12.  Designated by the [TestMethod] attribute. 1 – n test methods per Unit Test class. 1 method per major application requirement. 1 method per public property/method/web method as well (in most cases). A test method should test a single requirement only. A test method should be atomic.
  13. 13.  IMPORTANT: Test methods must run independently of one another!!! Test methods can run in any order, therefore their operation cannot rely on state set by other test methods. Test methods expecting any state or environment setup should always have that set by ClassInitialze or TestInitialize. It is not recommended that any state be setup in the test method itself. Avoid doing so whenever possible.
  14. 14.  Test method names should clearly indicate the purpose of the test. For tests that test a public property/class method/web method, use the name with the word Test appended. An example of this would be “ConnectionStringTest”. For tests that test a specific requirement, use a clear requirement descriptor with the word Test appended to the end. An example of this would be “AllFilesProcessedTest”.
  15. 15.  Tests are deterministic. For a given environment state and known input value(s), an expected result is the outcome of an assertion. Hard-coded, expected values are normal/acceptable. Use the Assert class to check for various expected conditions/state such as equality, inequality, Boolean value, etc. Use the ExpectedException attribute of the test method to insure that a specific exception is thrown if the application should throw it for expected errors. If anyAssert method fails within a test, the test result will indicate failure. Unhandled exceptions are treated as failures
  16. 16.  Test methods can have information about resources they should use passed to them via attributes. The [DeploymentItem] attribute is used to specify deployment items such as a file or a directory to be used by a specific test. The [DeploymentItem] can also be applied to an entire test class. There can be multiple deployment items specified for a method or class. The [TestProperty] attribute is used to pass general name/value pair data to a test. Test methods can have multiple test property attributes specified.
  17. 17.  Best practice to have a good comment header for each test method to clearly indicate what is being tested. Should include the following: Purpose: Short description of aspect of application being tested. Prerequisites: Assumptions being made before test can run such as a database with certain data exists and is accessible to the test project. Test Data: Data being used and list of possible values or ranges. Steps: List of steps in test. Remarks: Any pertinent information about test not covered elsewhere but important to note.
  18. 18.  Usually created manually in test class by developer. Skeleton tests can be generated by Visual Studio for existing code. Generated tests can be created only for properties/methods in existing code classes or interfaces. Tests for specific requirements must be coded manually.
  19. 19.  There are several ways to add tests to a test project. To add a standard unit test class to your project, go to the “Test” main menu option of the VS 2010 IDE and select “New Test” Select the “Unit Test” item from the dialog box. (The other test types available in VS 2010 are not covered in this presentation). Give your test class a descriptive name and click OK.
  20. 20.  To add your own unit tests to class, add public void, parameterless methods decorated with the test method attribute. Any method decorated with the [TestMethod] attribute is considered a test by the unit testing framework if it is contained within a test class. Uncomment the initialize/cleanup methods in the region “Additional Test Attributes” if you wish to use and customize them for your test class.
  21. 21.  To generate tests from existing code, use the Unit Test Wizard. From the “Test” main menu option of the VS 2010 IDE, select “New Test”. Select the “Unit Test Wizard” option. Select your unit test project from dropdown list. Click OK to run the wizard.
  22. 22.  Expand the namespaces for your application assembly in the tree view on the wizard. Expand the classes in the tree view to view the individual proprties/methods of the classes. Select the classes and/or methods for which you would like to generate unit tests. Click the settings button if you would like to modify the default code generation settings. Click the Add Assembly button if you want to generate tests from an additional assembly. Click OK to generate skeleton tests.
  23. 23.  Additional tests are easily added to existing test class from application project. Right-clicking from within your application class method gives context menu option to “Create unit test”. Wizard automatically selects current method in application (where cursor is positioned) and chooses the same target class in the test project as existing test methods for application class.
  24. 24.  The Test List Editor window is the easiest way to manually run tests from the Visual Studio 2010 IDE. They can also be run from the command line in an automated fashion, from the Microsoft Test Manager application (both not covered in this presentation), from the Test Results window of the VS2010 IDE and from the Test View window of the VS 2010 IDE.
  25. 25.  From the “Test” main menu option of the VS 2010 IDE, select “Windows” Choose the “Test List Editor” option The test list editor window gives you the ability to group, run and debug your tests and then view the results of the test run’s execution.
  26. 26.  Test results are displayed in the Test Results window. This window is automatically displayed by the Test List Editor when your test run completes All tests have a green “Passed” icon by them if the tests executed correctly Any test with a red “Failed” icon should be debugged to determine the reason for failure.
  27. 27.  Test result window displays a summary of all of the test results. Clicking an individual result in the results grid will open the results detail window and display more detailed information about the outcome of the selected test. Test Result Detail window provides detail error information if a test fails.
  28. 28.  Each test run creates a log of the results of the test run execution. Visual Studio will save the last 25 test runs by default. Different test runs can be viewed in the Test Results window. Test Results window will load most recent run by default. Select run name from dropdown at top of test results window to view other than most recent test run.
  29. 29.  Unit tests help with regression testing so that you may determine if new requirements/code changes have broken existing functionality in the code. The VS 2010 test framework includes a feature called Test Impact Analysis which helps to streamline regression testing. You can use Test Impact Analysis to record a baseline after initial unit test development. From then on, Test Impact Analysis will keep track of what tests need to be run after code changes to insure proper regression testing without running all of the tests.
  30. 30.  Visual Studio 2010 testing framework makes it easy to create and run unit tests for your Visual Studio 2010 projects. Good unit tests must follow industry standard best practices. Visual Studio does not impose any restrictions on what you put in your test. It is incumbent on the unit test creator to write good tests.
  31. 31.  us/library/dd264975.aspx us/library/ms182409 iting-great-unit-tests-best-and-worst-practises/
  32. 32.  nit-testing-best-practices-techniques.html tackoverfl08-20 us/magazine/cc163665.aspx w-Version-Of-Rhino-Mocks
  33. 33.  05/04/more-on-unit-testing-testcontext.aspx http://www.c- driven-development-tutorial-in-C-Sharp-4-0/ us/vstudio/ff718185.aspx programming/close-these-loopholes---reproduce- database-errors/