Skip class from UnitTest - Visual Studio 2019 - unit-testing

How to Skip class from unittest in visual studio 2019
ClienteRepositoryTests extends RepositoryTestBase.
RepositoryTestBase is a base for all unittests. I'd like that RepositoryTest dont show in Test Explorer

Make the base class abstract.
Both XUnit and MSTest (and probably other frameworks) use reflection to find correctly decorated methods (or types) and have to instantiate them in order to run the tests. If your class is abstract, the method will still exist on the derived type (still decorated as a test method), but the base class cannot be instantiated.

Related

How can I open class only to test class?

I'm mainly a Java developer and wonder about structure when writing unit test in kotlin,
Assuming there's no package-private in kotlin
private to restrict visibility to the file
internal to restrict visibility to the module
How can I open class only to test class ?
Must I write test inside kotlin class or open class to all module (internal)?
What's the kotlin way to open method for unit test only?
EDIT
Found similar question/request in kotlin discuss by #bentolor:
How am I supposed to do unit / whitebox testing properly? I want to write test code which tests class-internal functionality which I do not want to expose to other classes except my test class at all.
The package protected visibility is an excellent way to achieve this. Whereas Kotlin now requires me to make these methods effectively public and litter the visible API of my component all-over the project be able to test them.
In my view internal is more or less public as it has a much larger scope. Most projects have sth. around 1 - 5 “modules” in the Kotlin sense.
Really strongly asking/advocating for package-local visibility here.
Formally it is not possible to do this honestly on JVM, because class couldn't be open for subset of possible interiters.
However it can be partially done by the following trick:
open class SomeClass internal constructor(val configurableParameter: Int) {
companion object {
private const val defaultInput = 123
fun create() = SomeClass(defaultInput)
}
}
The constructor of this class can be called only from the same module (or from tests). And class is public, so anyone can use it. However from external modules you have only two ways of the class construction: companion object or reflection.
And finally you couldn't inherit from this class at any other modules, because constructor is internal.
For Android developers only, there's AndroidX VisibleForTesting annotation
Denotes that the class, method or field has its visibility relaxed, so that it is more widely visible than otherwise necessary to make code testable

How to find derived classes in a solution in Visual Studio 2013?

In Visual Studio 2013 when I go to Class View, then search for a class, then expand "Derived Types" folder it shows me only the classes derived from this class in the current project, but not the whole solution. Is there a way to find all the derived classes in the solution in Visual Studio 2013 or perhaps some plugins? The language in focus is C++ (unmanaged).
I have the same question. So far I am making do with a regular expression that matches part of the declaration of the derived class:
:\s*(public|private|protected)?\s+ClassName
For example when searching for all classes derived from class A in the following:
class B : public A {};
The regular expression tries to match the : public A. Note this will not work when A is not first base class in the list of base classes. Also does not work for indirect descendants.

Mocking internal classes with Moq for unit testing

Say I have a class "ClassA", which has a dependency on a class "ClassB" (injected into the constructor of ClassA). I want to mock ClassB so that I can test ClassA in isolation. Both classes are internal.
Correct me if I'm wrong but it looks like Moq can only mock a class if it is public, it has a public parameterless constructor, and the methods to be mocked are public virtual. I don't really want to make these classes publicly visible. Am I missing something with Moq, or is it just not suitable for what I want to do?
I guess I could create an interface (say "IClassB") that ClassB implements, inject that into ClassA, and mock the interface instead. ClassB can still be internal (although I realise the interface methods would have to be public). While this would work, I feel uneasy about creating lots of interfaces, whose only purpose is to support unit test mocking. Thoughts?
You could make internals visible to Moq by adding InternalsVisibleToAttribute in your project's assembly.cs, like this:
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")]
Why "DynamicProxyGenAssembly2" and not "Moq"? It's the name of dynamic assembly created to contain dynamically generated proxy types (all of this is handled by yet another library, Castle's DynamicProxy) which is used by Moq. So you expose types to dynamic proxy assembly, not to Moq itself.
But, what's the point of mocking class if there's no overridable member? You won't mock anything and all calls will use actual implementation. Your second solution,
I guess I could create an interface (say "IClassB") that ClassB implements, inject that into ClassA, and mock the interface instead.
is what I would normally do. Its purpose is much more than "to support unit test mocking" - it helps you build losely coupled components, which is always something worth aiming for.
Also, you can add this into .csporj file.
<ItemGroup>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>DynamicProxyGenAssembly2</_Parameter1>
</AssemblyAttribute>
</ItemGroup>

link a test-class to its main class

I routinely create, for each Java class, a corresponding test class. However, it seems that, from the compiler point of view, they are not linked in any way. Of course I can link them with mutual referrals in Javadoc comments, however, I wonder if there is a more standard way to tell the world "Class A is the JUnit test of class B" and "class B is the main class tested by class A".
You can't really have any compile-time reference since you typically distribute main code without test code. There is only a naming convention. For a class named Foo I use:
FooTest for unit tests
FooSomeSpecificPartOrBehaviorTest for unit tests of only a subset of class behaviors.
FooIT for integration tests
This convention is pretty standard and for example in IntelliJ IDEA Ctrl + Shift + T allows you to quickly navigate between main and test classes, only based on naming convention.
Usually the "connection" between the class under test and the test class is established via a naming convention: SHA1DigestTest contains the test for the SHA1Digest class.

inheritance in MS C++ using Visual Studio 2010

i have been going through the source code of an application which i downloaded from sourceforge.
i have a certain method which requires a pointer to an abstract class as a parameter.
class A;//abstract class
B::method(A *)
i cant create an object of A since its abstract.
so i can only pass pointer to object of child class of A in the B::method().
Now the problem is there are many classes in the source.
my problem is that how do i know which are the child classes of a parent class in visual studio 2010?
i have tried "find all references" for a virtual method of the abstract class (because its definition should be in the child class!) but without luck.
hope i could make my question clear!
Maybe "Solution Explorer" -> Your project -> Right Click Menu -> "Class Diagram" is what you're looking for ?
Another suggestion - try looking for " : public YourBaseClassName" strings and then "unwind" the inheritance. The usability certainly depends on the depth of your hierarchy. If there are multiple intermediate classes or multiple inheritance, then... Maybe, look at Eclipse+CDT ?