Team System Developer Center > Visual Studio Team System Forums > Visual Studio Code Analysis and Code Metrics > CA1053 (Remove the public constructors from [className]) in test class...
Ask a questionAsk a question
 

AnswerCA1053 (Remove the public constructors from [className]) in test class...

  • Sunday, November 05, 2006 6:36 PMThe Samster Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I have a class decorated with the [TestClass()] attribute. It was auto-generated by Visual Studio's unit testing framework. I'm getting a CA1053 error when I run code static analysis. Can this be fixed? Will it affect the unit testing generated code or render it unusable?

    Thank you,
    Sammy

Answers

  • Monday, November 06, 2006 5:01 AMDavid M. KeanMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    I'm not sure whether removing the public constructoir in this situation will prevent the Unit Testing Framework from running the AssemblyInitialize method. Even though it is static, the Framework might require that all [TestClass] have a public constructor.

    Try changing the class to a static class and see if that stops AssemblyInitialize from being run:

    [TestClass]
    public static class DatabaseSetup
    {
        [...]
    }

All Replies

  • Sunday, November 05, 2006 7:43 PMDavid M. KeanMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    This warning should only fire if you don't have any instance members. Methods marked with the [TestMethod] must be instance methods for the Unit Testing Framework to run them, so do you have any tests within this class?

     

  • Monday, November 06, 2006 1:32 AMThe Samster Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    I don't think this class is meant to have any TestMethods in it. It's auto-generated in a Database unit testing project and is never modified again. Whenever I add more tests they're created into their own classes. Here's the code:

    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Configuration;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using Microsoft.VisualStudio.TeamSystem.Data.UnitTesting;

    namespace DatabaseTests
    {
      [
    TestClass()]
     
    public class DatabaseSetup
     
    {
        [
    AssemblyInitialize()]
       
    public static void IntializeAssembly(TestContext ctx)
        {
         
    // setup the test database based on setting in the configuration file
         
    DatabaseTestClass.TestService.DeployDatabaseProject();
         
    DatabaseTestClass.TestService.GenerateData();
        }
      }
    }

    Thank you,
    Sammy

     

  • Monday, November 06, 2006 5:01 AMDavid M. KeanMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    I'm not sure whether removing the public constructoir in this situation will prevent the Unit Testing Framework from running the AssemblyInitialize method. Even though it is static, the Framework might require that all [TestClass] have a public constructor.

    Try changing the class to a static class and see if that stops AssemblyInitialize from being run:

    [TestClass]
    public static class DatabaseSetup
    {
        [...]
    }

  • Wednesday, September 09, 2009 8:35 PMsdokic Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Add this to your code. This will remove public constructor warning/error.


    internal  DatabaseSetup()
    {
    }
  • Sunday, October 25, 2009 12:03 PMAmer jamaeen Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    make your class static if you have no instance from it.