1

Are C# namespaces hierarchical?

Background: I'm developing a test framework and I have per-namespace setup fixtures. e.g. a SetUpFixture in namespace A and one in namespace A.B.

Actual question: If I call TestRunner.RunAllTestsInNamespace("A.B") - should the test runner only call the SetUpFixture in namespace A.B or also the one in namespace A?

(or the other way round: If I call TestRunner.RunAllTestsInNamespace("A") - should the test runner also execute all the tests in namespace A.B?

D.R.
  • 20,268
  • 21
  • 102
  • 205
  • 1
    Relevant: http://stackoverflow.com/questions/125319/should-usings-be-inside-or-outside-the-namespace – It'sNotALie. Jul 16 '13 at 21:12
  • @Jon: Your answer is not reflected in the question pointed out by newStackExchangeInstance. Are you sure your answer is correct? [Jon has removed his comment] – D.R. Jul 16 '13 at 21:16
  • @newStackExchangeInstance: As they seem hierarchical, would you suggest executing all setupfixtures in the current namespace and above namespaces? – D.R. Jul 16 '13 at 21:19
  • Not sure what you mean, could you try rewording that question? (Your last comment). – It'sNotALie. Jul 16 '13 at 21:20
  • Hope my rewording helps :-) – D.R. Jul 16 '13 at 21:23
  • This is now more of a P.SE question, as this is an opinion question. IMO yes, but you need to move this. – It'sNotALie. Jul 16 '13 at 21:24
  • Are you sure that is an opinion question? If C# namespaces are per definition (e.g. standard document) hierarchical, I guess it is out of question and a MUST?! – D.R. Jul 16 '13 at 21:25
  • Uhm... not really, as it depends on whether it's best practice to do it or not. – It'sNotALie. Jul 16 '13 at 21:26
  • 2
    Namespaces are hierarchical. There also seems to be a completely different question related to testing struggling to get out, but I can't see it because my mind just keeps looking at the first sentence and answering "Yes. Now move along to someone else's question." – Ben Voigt Jul 16 '13 at 21:34

3 Answers3

2

Regarding your question:

If I call TestRunner.RunAllTestsInNamespace("A.B") - should the test runner only call the SetUpFixture in namespace A.B or also the one in namespace A?

Assuming you have the structure:

A.Setup1
A.Test1
A.B.Setup2
A.B.Test2

If you do some simple tests in an existing framework, such as NUnit, you will find the behavior that is followed appears to be:

  • Running A.Test1 executes A.Setup1 and A.Test1
  • Running A.B.Test2 executes A.Setup1, A.B.Setup2, A.B.Test2

This is the behavior of NUnit. Others may behave differently.

You might want to fully investigate and understand the behavior of existing test frameworks (NUnit, XUnit.Net, MSTest, others).

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
1

Yes. Namespaces are hierarchical.

Rob van der Veer
  • 1,148
  • 1
  • 7
  • 20
1

Yes namespaces are hierarchical, but then the question is API design. I guess it's up to you to define which behavior you want to implement.

What is sure is that it is the other way round (the second way in your question): Run("A.B") should surely not run tests in "A". Because A is not included in A.B --> A.B is included in A.

Why not have an optional parameter for that?

public void RunTests(string @namespace, bool runChildNamespaces = true)

With this API design:

  • the implementation is only slightly more complicated
  • you gain some flexibility
  • the intended behavior is very clear for the coder who calls the method.
jods
  • 4,581
  • 16
  • 20