Create a Test Project

  • Open Visual Stuio > Click ‘Create a New Project’
  • Search "xunit
    • Note: the general principles of unit testing can be applied to nunit project or even a regular Microsoft test project.
  • Select xUnit Test Project
    •  
    •  
    • Click Create

 

 

This will create a Default Class ‘UnitTest1’ and a default method as below

 

  • [Fact] Annotation
    • This is what tells the IDE, the program everything that here is just a unit test. Don't factor this into the entire runtime of the application.
  • Steps in Unit Test
    • A unit test usually has three steps to it.
      • Arrange.
        • A stage at which 
          • All data is prepared,
          • Test artefacts are prepared. 
          • Create objects or representations of whatever data we would expect to be testing against.
      • Act
        • Here we're going to actually simulate a call to the method that we hope should be called when we enact such an action.
      • Assert
        • Here we have to assert that this is the outcome with this input to this method. 
        • Is this happen Or Not?
          • If we said this should happen and it did not happen, that test will basically fail, show red. 
          • Then we'll have to either write the code to make it green

 

 

 

Test Explorer

  •  Visual Studio makes it very easy to navigate through tests because they give us a test explorer.
  • View > Test Explorer
  • What is the user of Test Explorer
    • An interface that 
      • shows us all of the unit tests that are pending, 
      • shows us all of the test projects, one, all of the unit tests in said projects.
      • Allow us to run these tests.

 

 


Related Question