Attributes used in Unit Test class in VSTS.
AssemblyInitializeAttribute : Identifies a method that contains code to be used before all tests in the assembly have run and to allocate resources obtained by the assembly. This class cannot be inherited. Only one method in an assembly may be decorated with this attribute. Usage
[TestClass] public class UnitTestInitializer { [AssemblyInitialize] public static void InitializeAssembly(TestContext context) { // Initialize AutoMapper AutoMapperBootstrapper.Initialize(); } }ClassIntializeAttribute : Identifies a method that contains code that must be used before any of the tests in the test class have run and to allocate resources to be used by the test class. This class cannot be inherited. Only one instance of this attribute may be applied to a method. Usage
[ClassInitialize] public static void InitializeClass(TestContext testContext) { // Inject objects that get instantiated in StructureMap calls outside of the test's control ObjectFactory.InjectTestInitializedAttribute : Identifies the method to run before the test to allocate and configure resources needed by all tests in the test class. This class cannot be inherited. This method marked with this attribute will run once for every virtual user iteration in the test. Usage(new MemoryCacheProvider()); }
[TestInitialize] public void InitializeTest() { var employeeRepository = new EmployeeRepository(new LookupRepository(), new ContactRepository(new UserRepository())); _employeeService = new EmployeeService(employeeRepository ); }TestMethodAttribute : Used to identify test methods. This class cannot be inherited. Usage
[TestMethod] public void GetEmployeeTest() { var emp = _employeeService .GetEmployee(empId); Assert.IsNotNull(emp ); Assert.IsTrue(emp.Id> 0); }The order in which the methods decorated with the above attributes will be run is :
- Methods marked with the AssemblyInitializeAttribute.
- Methods marked with the ClassInitializeAttribute.
- Methods marked with the TestInitializeAttribute.
- Methods marked with the TestMethodAttribute.
No comments:
Post a Comment