Change tests to use consistent namespaces
Namespace used in test projects are all over the place:
- default (previous version of System.Text.Encoding.CodePages tests)
- Test (PLINQ)
- $packageNamespace.Tests
- $packageNamespace.UnitTests
.... and potentially more.
Is there any guidance for what we should be using? The only potentially applicable guideline would be "Follow the style of the current project", which doesn't help much in the case of new work (or say what any canonicalization tool would change things to).
@weshaggard @CIPop @ellismg
Yes, there is guidance posted. The buildtools requires the project *.CSPROJ file be suffixed with ".Tests.csproj"
Please see the Http tests for example of multiple test projects with proper naming of namespace, CSPROJ filename and test assembly name.
https://github.com/dotnet/corefx/tree/master/src/System.Net.Http/tests
...yeah, but that only covers the .csproj name. It didn't actually affect the tests themselves, which was why I was asking for clarity.
The Http tests do seem to be using $packageNamespace.Tests, though.
I don't know that it is written down anywhere but my general guidance is to use $(Namespace of Type under Test).Tests.
I'm good with either $(namespace of Type under test).Tests or $(packageName).Tests.
I agree we should be consistent.
@Clockwork-Muse Thanks for bringing this up.
@weshaggard , @nguerrera - Sorry, I may have meant$(Namespace of Type under Test).Tests, essentially (Java coloring what I was seeing). Since I was anticipating package name matching declared namespace....
I'd second (or third?) that most of our tests are going with the "$(namespace of Type under test)" convention. Actually, a lot of our assembly names don't really match up with the declared namespace anyways (is this what you meant by "package name"?). And, usually, the assembly name is a bit longer than the root namespace, so it makes the test namespace even longer :smile: .
I'd be fine if we wanted to standardize on this.
OK, so I think we have consensus for "$(namespace of Type under test).Tests". Any volunteers to clean this up? @Clockwork-Muse were you thinking about contributing the change? If not, I'll mark this up for grabs.
@nguerrera - Sorry, I'm working through something else at the moment. I'll definitely do it for PLINQ and Encoding.CodePages (since I'm in those/have more changes for them), though.
Okay, I said I'm not doing this (and I'm not), but I was planning on doing it for everything I've previously touched. I will not go hunting for cases. So stuff I'll check includes:
- [ ] System.Collections - Complicated web of nested/missing/other namespaces (done, but too many indentation changes for PR).
- [x] System.Collections.Concurrent - dotnet/corefx#3452
- [x] System.Collections.Immutable - dotnet/corefx#3452
- [x] System.Collections.NonGeneric - dotnet/corefx#3452 - Some files lack a namespace.
- [ ] ~~System.Composition~~ Skipping for now, solution setup is unusual.
- ~~System.Diagnostics.Contracts~~ N/A - namespaces already correct
- [ ] System.Diagnostics.FileVersionInfo - Only changes related to missing namespaces.
- [x] System.Diagnostics.Process - dotnet/corefx#3453
- [x] System.Diagnostics.Tracing.Telemetry - dotnet/corefx#3453
- [ ] System.Globalization - Only changes related to missing namespaces.
- [x] System.Globalization.Extensions - dotnet/corefx#3455
- [ ] System.IO.Compression.ZipFile - Only changes related to missing namespaces.
- [x] System.IO.FileSystem - dotnet/corefx#3456 - Some files lack a namespace.
- ~~System.IO.Packaging~~ N/A - namespaces already correct
- [x] System.Linq.Parallel - part of dotnet/corefx#3132
- ~~System.Net.Http~~ N/A - namespaces already correct (although there are two test projects?)
- [x] Systen.Net.Requests - dotnet/corefx#3660
- [x] System.ObjectModel - dotnet/corefx#3457 - Some files lack a namespace
- ~~System.Reflection~~ N/A - namespaces already correct
- [x] System.Reflection.Extensions - dotnet/corefx#3458
- [x] System.Reflection.Metadata - dotnet/corefx#3458
- [x] System.Reflection.TypeExtensions - dotnet/corefx#3458 - Some files lack a namespace
- [x] System.Runtime - dotnet/corefx#3491 - Some files lack a namespace
- [x] System.Runtime.Extensions - dotnet/corefx#3491 - Some files lack a namespace
- [x] System.Runtime.Numerics - dotnet/corefx#3491
- [x] System.ServiceProcess.ServiceController - dotnet/corefx#3492
- [x] System.Text.Encoding - dotnet/corefx#3604
- [x] System.Text.Encoding.CodePages - dotnet/corefx#3604
- [ ] ~~System.Text.Encodings.Web~~ - Skipping: The original authors appear to be anticipating future changes in their test setup.
- [ ] System.Threading - Only changes related to missing namespaces.
- [ ] ~~System.Threading.Tasks.Dataflow~~ N/A - namespaces already correct
- [x] System.Threading.Tasks.Parallel - dotnet/corefx#3605 - one file with large indentation changes outstanding.
- [x] System.Xml.ReaderWriter - dotnet/corefx#3606
- [x] System.Xml.XmlDocument - dotnet/corefx#3606
(not everything may need fixing) I'll check stuff off as I submit PRs.
Okay, I've done just about everything I am for this. Where I've noted large indentation changes, I've pulled those off into another branch, so it's ready to be used in the combined PR.
Fixed System.IO.Compression with dotnet/corefx#3088. While looking at this, I noticed that there are 2732 tests in the repo that have no namespace at all. Adding a namespace to those will touch a large number of lines due to the re-indentation, so I think we should do them at once.
Would it be ok to separate tests by their type? I think the following namespace identification makes sense given the various types of tests that we have for System.Net:
- Various types of tests
- namespaceUnderTest.Unit.Tests
- namespaceUnderTest.Functional.Tests
- namespaceUnderTest.Performance.Tests
- Common code:
- namespaceUnderTest.Common.Tests
We can't change to move everything into a single csproj file since that won't allow for UnitTesting which for Networking code is essential. Not having some simple way of differentiating the test namespace will make it harder to understand which file can or cannot be included in other projects (FunctionalTest code should never be used in a Functional tests for example as it may introduce actual system calls.)
In terms of System.Net tests, we already have code in src/Common\tests\System\Net that has helper classes listed under the System.Net.Tests namespace. So, we should consider that so if we decide to move those "Common" classes to "System.Net.Common.Tests", we need to update some other test libraries.
Personally, I like @CIPop idea of refining the original agreement. However, note that most tests in CoreFx don't have separate "Unit" vs. "Functional" tests like Networking. So, the majority of them are simply suffixed with ".Tests". I think that is ok, though.
So, perhaps we simply need to all agree on a refinement to the original proposal here and allow for the following namespaces for test classes/methods and/or Common test code as such to include @CIPop proposal above including this:
namespaceUnderTest.Tests
when those tests don't have separate breakouts for Unit/Functional/Performance.
This bug is confusing in that it is overly broad and there is no clear definition of done since nothing is preventing new code from violating rules fixed in different sub-folders.
Given the checkboxes in Clockwork-Muse's post is it still correct that System.Threading (and all sub-namespaces?) is the only area that remains to be fixed? Not sure of a good way to do this but IMO part of solving this issue should be preventing new test files being created with namespaces that don't conform to what is agreed upon. Maybe a Roslyn code analysis check that confirms that:
at most one namespace descendant from System.Threading is in the usings and if that exists that the namespace is "that using text".Tests
for those not explicitly "using" a more specific namespace either raise and error and force one to be specified (much easier I think) or analyze the test code and confirm that all symbols from the System.Threading.* namespace share a common subtree. Ie A.B.C and A.B.D are okay but A.B.C and A.E.D aren't. This of course would limit the cross namespace testing ability but would enforce that the tests don't span crazily wide ranges of functionality.
Closing, as the details are now very out-of-date. We welcome further clean-up in this area, but this issue isn't tracking it well.