1

What are the use cases of TestOf? I'm new to NUnit testing and from what I have seen, most people don't use this attribute that much.

From my experience, TestOf helped me to quickly identify what class is being tested and make the navigation between test and the production code easier. I think this is the main intention of using TestOf. Feel free to correct me if I'm wrong.

However , some test fixtures that I have seen use an approach where the test fixture is named in a way that it reflects the class it is testing. This is mostly done by adding 'Test' to the end of the class that is being tested. That is: SomeClass will have a test fixture named SomeClassTest

In these type of naming conventions, TestOf will be completely useless.

Would be great if someone can clarify the actual usage of TestOf attribute

    1 Answer 1

    2

    TestOf is a formal way of specifying what's tested. If there was a valid type referred to is checked at compile time, and it provides some information which can be evaluated at run time or by some IDE (for example, to allow some reverse lookup or easier refactoring).

    In contrast, any kind of naming convention for indicating the tested class will only be informal. When you have a typo in the name of a test fixture the compiler won't notice, and when you rename a tested class, you will have to remember to change the test fixture's name accordingly, no tool will tell you.

    In these type of naming conventions, TestOf will be completely useless

    It might be somewhat redundant, but definitely not "completely useless".

    If you need the kind of formality provided by TestOf in your code base is a different question. I am currently working with a code base with ~800 Nunit tests and no TestOf attributes at all, and still don't think we missed something important. Maybe the situation becomes different once we will reach 8000 tests, I don't know.

    2
    • Thanks doc. Now I'm clear about the advantages of using TestOf. But still, it's a bit cloudy about whether and when to use it. From your last line, I'm assuming it's only useful if there is a large number of test cases.
      – LNTR
      CommentedDec 24, 2024 at 15:50
    • 2
      @LNTR: NUnit has ~50 different attributes, but I don't see any necessity to use them all, not even half of them. In fact, I prefer not to start with a solution when I don't have a problem. So when you regularly have the problem that you have issues to identify which classes are tested by which tests, then TestOf might be a tool which can help a little bit (if it really helps you is something you need to try out, I cannot tell you). If you don't have that problem, no need to use something just because "it is there".
      – Doc Brown
      CommentedDec 24, 2024 at 19:11

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.