Monday, December 5, 2011

MSTEST - UTA004: Illegal use of attribute on testmethod

Have you seem this error UTA004: Illegal use of attribute on testmethod. The TestMethodAttribute can be defined only inside a class marked with the TestClass attribute. while trying to run a mstest unit test.
The common scenario when this occurs is if there is a method with [TestMethod] attribute but the class in which it is defined does not have [TestClass] attribute.
However, even if you add the [TestClass] attribute and the error does not go away, the reason is that the class is not public.

Hence, make sure that the class is public and it has [TestClass] attribute.

22 comments:

  1. Awesome! Thanks so much. I guess it needs to be public so that the test runner executable can see it.

    ReplyDelete
  2. Awesome! Thanks so much. I guess it needs to be public so that the test runner executable can see it.

    ReplyDelete
  3. Thanks that fixed the problem for me.

    ReplyDelete
  4. Thanks! Saved me a lot of frustration.

    ReplyDelete
  5. Thanks.. This information helped me.

    ReplyDelete
  6. It helped a lot. Thanks!

    ReplyDelete
  7. Thank you very much :)

    ReplyDelete
  8. Thanks , that did the trick !!!

    ReplyDelete
  9. There's another scenario where you can get this error with a TestClass attribute defined - if you have a DeploymentItem attribute with a disallowed output path like @"\" then you'll get this error.

    ReplyDelete
  10. There's another scenario where you can get this error with a TestClass attribute defined - if you have a DeploymentItem attribute with a disallowed output path like @"\" then you'll get this error.

    ReplyDelete
  11. Same issue I got and resolved.. Thank you!

    ReplyDelete