How to exclude a class constructor using a dotCover filter? - dotcover

What should go in the "Function mask" field in dotCover's "Edit Coverage Filter" dialog? I've tried "Foo" and "Foo*" with no effect.
example:
public class Foo
{
public Foo(int x, int y)
{
// how can I exclude this code from the code coverage calculation?
}
...
}

First of all, it's worth mentioning that dotCover analyses complied assemblies, not the source code, to generate its coverage reports. Any C# constructor (irrespective of its name in C#) is compiled into a method named .ctor (or .cctor if the constructor is static). That's why dotCover will never see a method called Foo(int, int) in your example.
If you want to filter out the constructors of the Foo class, you need to type the following in "Edit Coverage Filter" dialog:
Module Mask: YourNamespace
Class Mask: YourNamespace.Foo
Function Mask: .ctor
Hope this helps.

Related

How to create a unit test class

I inherited a solution with 4 projects: Front-End Project, Business Project, Data Project and a Test Project.
The test project is quite... let's say empty... and now after I changed a few things on some searches methods of the business class I would like to generate some tests to validate the changes i've made.
So my question is: Is there a automatic way to generate a "empty frame test class" to test my actual code? Something like "right click the class you want to test and click generate test class and choose the project where it will be created" maybe?!?
Details:
I'm using VS 2012 Ultimate
There's no tests for the class I'm working on
There is built in functionality that allows you to create unit test classes. I am not sure if that also works in combination with NUnit though.
Anyway, I never used it. What I do is:
add a test class to the test project
decorate the class with the [TestFixture] attribute
write the first method of what I want to test
decorate the method with the [Test] attribute
write the test
and start the TDD cycle.
A typical test class skeleton will look like this
using NUnit.Framework;
namespace Tests.Framework
{
[TestFixture]
public class SomeClassTests
{
[Test]
public void AMeaningfulTestMethodName()
{
// the test
}
}
}
I also have Resharper at my aid so that I can run the test from visual studio straight away.
Since it's so little effort for me to add a new test fixture to the project, I don't see the need of adding it via templates. The most annoying part of templates is that they overgenerate. Templates will generate [SetUp] and [TearDown] fixtures which I don't always need. I like to keep my classes as clean as possible. But it's a matter of taste.
Here are some links that you might find helpful if you want to:
save your own predefined test class template
want to use the built in functionality of visual studio
follow a msdn walkthrough regarding the topic

How to write test code before write source code when they are objects dependencies?

I'l will take an example: requirement: construct a piece of material following these rule: among given < 100 => material is green
100 < among <300=> material is yellow
among > 300=> material is red
My test class:
class MaterialTest{
MaterialConstructor materialCons
public void testBuild(){
materialCons.setAmount(150);
Material material=materialCons.build();
assertEqual(material.getColor(),"Yellow");
}
}
From know i don't how to implemented materialCons.build(); After design and analysis i've implemented MaterialCosntructor as follow:
public class MaterialConstructor {
private Helper1 helper1;
private Helper2 helper2
MaterialConstructor (Helper1 help1, Helper2 help2){
.................
}
public Material build(Double among){
part1=helper1.builpart(among);
return helper2.construct(part1);
}
}
In my initial test class i must includes code like this:
helper1=createMock(Helper1)
helper2=createMock(Helper2)
materialCons=new MaterialConstructor (helper1, helper2)
.....................
expected(helper1.builpart(150)).andReturn(some result)
expected(helper2.construct(some result)).andReturn("Yellow")
as result we can get this updated class test:
class MaterialTest{
MaterialConstructor materialCons
public void testBuild(){
helper1=createMock(Helper1)
helper2=createMock(Helper2)
materialCons=new MaterialConstructor (helper1, helper2)
expected(helper1.builpart(150)).andReturn(some result)
expected(helper2.construct(some result)).andReturn("Yellow")
materialCons.setAmount(150);
Material material=materialCons.build();
assertEqual(material.getColor(),"Yellow");
}
}
hence my test code will be update after writen my source code!
This will appear many time ( since it's difficult at specification time to know which dependencies class you must use to solve your problem). Then unit-test class will be always obsolete after written source code!
Following a strict TDD workflow such as the 3 Laws of TDD would definitely put an end to your hesitations.
With it you can't have such things as "After design and analysis i've implemented MaterialCosntructor ..." since you are supposed to
Write only enough production code to pass a test
Which basically means : just return "Yellow" as your first implementation.
Then you would add more tests (green, red material) and incrementally design your class to handle these additional cases.
No I think your idea is absolutely brilliant to have unit test cases for testing objects. I do not think the test class will be obsolete after written source code because in your case you have designed the class members and class methods.. the only thing that seems to be stopping you is to write the EasyMock testcases.. Here is some help material, maybe it will make things more clear for you IBM Developer Works Simulation Page ..
There is also one more hyperlink which is by EasyMock readme themselves EasyMock ReadMe

Can I make Intellij Idea 11 IDE aware of assertEquals and other JUnit methods in Grails 2.0.x unit tests?

I find it very odd that with such excellent Grails integration, Idea does not recognize standard JUnit assertion methods in Grails unit tests. I created a brand new project and made one domain class with corresponding test to make sure it wasn't something weird with my larger project. Even if I add a #Test annotation, the IDE does not see any assertion methods
#TestFor(SomeDomain)
class SomeDomainTests {
#Test //thought adding this, not needed for Grails tests, would help but it doesn't
void testSomething() {
assertEquals("something", 1, 1); //test runs fine, but IDE thinks this method and any similar ones don't exist
}
}
I have created an issue in IntelliJ bugtracker: http://youtrack.jetbrains.com/issue/IDEA-82790. It will be fixed in IDEA 11.1.0
As workaround you can add "import static org.junit.Assert.*" to imports.
Note: using "assert 1 == 1 : 'message'" is preferable than "assertEquals('message', 1, 1)" in groovy code.
Idea has problems if you use 'def' to define a variable (so it's type is not known) and then you try to pass it to a Java method which is strongly typed. Because it can't infer the type.
So it will give a message with words to the effect of "there is no method assertEquals() that takes arguments with type String, null, null".
I wouldn't expect this message in the example you give (because you are using ints directly, not a dynamically-typed variable) but I thought you might have missed it when trying to create a simple example code snippet for the question.
With the #TestFor annotation an AST will add methods to you test class and IDEA does not catch these methods.
You have two options:
Make the test class extends GrailsUnitTestCase.
Add dynamic method to your test class.

Resharper or nUnit test count for reflective tests

I have a a number of tests based upon reflection. The reflective test work on a given assembly for a give type, i.e. look in the service.dll for everything based upon IService for example. I have done it this why because I need to pipe reference types into the tests that I can't do with TestCase attribution. The code basically looks like the following:
public static void TestRunnerforTypeList(Type baseType, Func<Type, string, bool> excluder, Action<Type, string> mockResolver, Dictionary<Type, Dictionary<Type, Mock>> mocks, Action<dynamic, Type> assertor, string methodName)
{
foreach (var type in GetTypesToTest(baseType))
{
if (excluder(type, methodName)) continue;
dynamic objectToTest = CreateInstance(type, mocks);
mockResolver(type, methodName);
assertor(objectToTest, type);
}
}
A call to this would look like the following:
[Test]
public void Positive_outcome_for_Get()
{
GeneralTestRunner.TestRunnerforTypeList(typeof(IService<,,>),
_serviceFactoryContext.ExcludeTypeForMethod,
_serviceFactoryContext.ResolvePositiveMockSetup,
_mocks,
(service, type) => Assert.IsNotNull(service.Get(1)),
"Get");
}
It's simple assertion but you get the idea. This way I get the benefit of TestCase attribution but with reference types, like mocks, being piped in.
However I have other places where I use TestCase attibution and Resharper picks up these up and increases the number of test in the test session, the refective ones don't.
My question is there a way of telling Resharper (or nUnit) that the number has increased every time the above assertor action is called?
Thanks in advance ;)
The only way I've found to do this is with Resharper 6 and dropping the reflective aspect and use a TestCaseSource attribute. The TestCaseSource would point to a static IEnumerable yielding a factory reference type for each test. See documentation below:
http://www.nunit.org/index.php?p=testCaseSource&r=2.5
The only issue compared to the question implementation is the reflective test will fail if programmer doesn't create mocks for the instance creator, however the type is still picked up (if it's in the same assembly). Therefore if a lazy programmer creates a new type based upon IService (for example) with no tests, if fails, out the box.

Visual studio 2010 template for new UnitTest

What ways do I have for creating a unit test template like this? I'm using visual studio 2010 and Resharper 5.
using NUnit.Framework;
namespace SolutionName.Core
{
[TestFixture]
public class ClassNameTests
{
[Test]
public void test()
{
}
}
}
Yes it's possible. This answer requires Reshaper to be installed.
First, go to the Menu and choose Resharper => Live Templates. There switch to the File-Templates tab. Add a new template there.
Give the template a description like 'UnitTest'
Give it a default-name like 'Test.cs' and choose 'File name must be a valid identifier'
Change the availability to 'in C# project ...'
Copy and paste your template into the text-field. Then replace the class-name with a variable. Variables are surrounded by $-characters. The $END$ marks where the cursor is when your done. Like this:
using NUnit.Framework;
namespace SolutionName.Core
{
[TestFixture]
public class $ClassNameTests$
{
[Test]
public void test()
{
$END$
}
}
}
Right next to the text-field is a list of the variables (you might need to extend is, its sometimes really small). There the ClassNameTests-variable appears. Choose the macro 'Current file name without extension'. Now the template is done, so store it
Go to your solution, right-click there and choose Add -> New From Template -> More... . There select your template. Make sure that you check the 'Add to quicklist' option and confirm. Now you can enter the class-name for the test and Resharper creates the file for you.
After that the template will be under Add -> New From Template -> Your Template Name
You can probably improve my solution with additional variables & macros. Play with it. Add variables to your template by surrounding a name with $-chars. Then choose a macro for the variable on the list next the the template-text.