How to extend/migrate TraitAttribute to XUnit v2 and why has become sealed

xUnit

In these days I was testing xUnit for the first time, and as a greedy developer, I started with the pre-release v2. Everything was great, except that I was trying to categories my tests in unit, integration, manual, and so on, to let me separate the CI tests from the ones, and I found misleading documentation out there, actually it was simply because there’s some breaking changes passing from v1 to v2, and one is that TraitAttribute is now sealed.

In xUnit v1 and v2 there’s the Trait attribute than can be used to add any kind of description above a test method and that can be read from visual studio test explorer and of course from gui/consoles as well. This description can be useful to let you run just a “category” of tests. See “Using Traits with different test frameworks in the Unit Test Explorer” for more information about Traits in Visual Studio 2013.

Trait and Visual Studio Test Explorer

The annoying part is that Trait takes two strings as name/value pairs, and needs to be copied all around your tests:

public class MyTests1
{
    [Trait("Category", "CI")]
    [Fact]
    public void Test1()
    {
    }

    [Trait("Category", "CI")]
    [Fact]
    public void Test2()
    {
    }

    [Trait("Category", "CI")]
    [Fact]
    public void Test3()
    {
    }
}

One convenient way is to put the Trait attribute on the class itself, in that way every single method will “inherits” that attribute, and with the latest version of visual studio + updates, you will get the expected behavior in the test explorer:

[Trait("Category", "CI")]
public class MyTests2
{
    [Fact]
    public void Test1()
    {
    }

    [Fact]
    public void Test2()
    {
    }

    [Fact]
    public void Test3()
    {
    }
}

But a more convenient way could be to define our CI attribute, and in v1 was easy as inherits:

public class CIAttribute : TraitAttribute
{
    public CIAttribute() 
        : base("Category", "CI")
    {
    }
}

and you can straight use it:

public class MyTests1
{
    [CI]
    [Fact]
    public void Test1()
    {
    }

    [CI]
    [Fact]
    public void Test2()
    {
    }

    [CI]
    [Fact]
    public void Test3()
    {
    }
}

[CI]
public class MyTests3
{
    [Fact]
    public void Test1()
    {
    }

    [Fact]
    public void Test2()
    {
    }

    [Fact]
    public void Test3()
    {
    }
}

…but how to do the same in xUnit v2, where the TraitAttribute become sealed? And why has become selead?

I ask this on a github issue and Brad Wilson (a team member of xUnit) replied me a really exhaustive explanation:

Test discovery in xUnit.net can happen without binaries (Resharper and CodeRush can discover tests as you type, by looking at the source code, even when that code doesn’t necessarily compile).

In order to support this (not being able to run code), it requires that we seal the attributes, so that discovery can be based on things that are known only from the source, and not from running compiled code.

Let’s take a common example: someone wants to make [Category("...")] which is the equivalent of[Trait("category", "...")]. Without a sealed attribute, you would write this:

public class CategoryAttribute : TraitAttribute
{
    public CategoryAttribute(string value) : base("category", value) { }
}

The problem is that Resharper or CodeRush cannot run this code, they can only look at the source. Simple examples like this may be possible, but it doesn’t take long to come up with a scenario where it becomes non-trivial and the only recourse would be to run code and see what happens.

So the tradeoff we make here is to require a bit of known-to-be-compiled code which can inspect the[Category("...")] code and return the correct trait value(s). By sealing the attribute, it says that someone who wants to write CategoryAttribute also needs to write the discoverer that can make sense of what that means without being able to run the code in question.

We have an example which does exactly this: https://github.com/xunit/samples/tree/master/TraitExtensibility

and here it is the migration of CIAttribute:

[TraitDiscoverer("ClassLibrary1.CIDiscoverer", "ClassLibrary1")]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true)]
public class CIAttribute : Attribute, ITraitAttribute
{
}

public class CIDiscoverer : ITraitDiscoverer
{
    public IEnumerable<KeyValuePair<string, string>> GetTraits(IAttributeInfo traitAttribute)
    {
        yield return new KeyValuePair<string, string>("Category", "CI");
    }
}

Pay attention on [TraitDiscoverer("ClassLibrary1.CIDiscoverer", "ClassLibrary1")], the first argument is “The fully qualified type name of the discoverer (f.e., ‘Xunit.Sdk.TraitDiscoverer’)” and the second argument is “The name of the assembly that the discoverer type is located in, without file extension (f.e., ‘xunit.execution’)“, it took me a while to figure it out because as usual I should RTFM first :)

How to open and close Flyouts in Universal apps using MVVM

Originally posted on Around and About .NET World:

Some times ago we talked about how to open attached Flyouts and close them using custom behaviors and MVVM. This was necessary because the Flyout control doesn’t expose properties to control its visibility, so we need to use its ShowAt and Hide methods explicitly.

While this approach works correctly, we can obtain the same result in a more elegant way with two simple Attached Properties. The following is a generic version of the implementation shown in the article Using Windows 8.1 Flyout with MVVM and works with every control, not only buttons (remember, in fact, that we can attach a Flyout on any FrameworkElement).

Let’s start creating a FlyoutHelper class:

This code adds an IsOpen Attached Property to flyout objects. When it changes (lines 22-39), we check whether we want to open or close the Flyout: in the first case, we call the ShowAt method (lines 32), that actually…

View original 234 more words

Git: Aliases

git-6-728

With aliases, you can avoid typing the same commands over and over again. Aliases were added in Git version 1.4.0. I found in githowto.com/aliases a great set of aliases that I really use everyday and I would like to share.

One of the most useful alias is “git hist“, it shows you all the last commit, showing the branch tree and the commit message, all in a short form (remember to hit ‘q’ to exit the hist command).

git-hist

In windows you just need to add the following section to the .gitconfig file under %HOMEDRIVE%%HOMEPATH% folder (usually is something like ‘C:\Users\michael\.gitconfig’).

[alias]
  co = checkout
  ci = commit
  st = status
  br = branch
  hist = log --pretty=format:\"%h %ad | %s%d [%an]\" --graph --date=short
  type = cat-file -t
  dump = cat-file -p

or you can run these commands in the command-line prompt:

git config --global alias.co checkout
git config --global alias.ci commit
git config --global alias.st status
git config --global alias.br branch
git config --global alias.hist 'log --pretty=format:"%h %ad | %s%d [%an]" --graph --date=short'
git config --global alias.type 'cat-file -t'
git config --global alias.dump 'cat-file -p'

If you don’t already have a .gitconfig file, this could be a default settings file you can use:

[merge]
	tool = kdiff3
[mergetool &quot;kdiff3&quot;]
	path = C:/Program Files/KDiff3/kdiff3.exe
[diff]
	guitool = kdiff3
[difftool &quot;kdiff3&quot;]
	path = C:/Program Files/KDiff3/kdiff3.exe
[user]
	name = Your Name
	email = youremail@me.com
[alias]
  co = checkout
  ci = commit
  st = status
  br = branch
  hist = log --pretty=format:\"%h %ad | %s%d [%an]\" --graph --date=short
  type = cat-file -t
  dump = cat-file -p

Sql Server: Backup all the databases of the instance

security8

They told me (Alessandro Alpi), to don’t blog this :) because there’s a lot of this stuff out there, but this my blog, and it is too cool when developing in load testing environment, and you need to backup, try load test data, and restore previous version, so forgive me about this:

sys.sp_MSforeachdb @command1 = N'BACKUP DATABASE [?] TO DISK = N''C:\_Databases\_Backup\Databases\?.bak'' WITH NOFORMAT, INIT'

Change the “C:\_Databases\_Backup\Databases\” into your default BackupDirectory found in the instance facets, to avoid security problem.

DON’T USE THIS CODE IN PRODUCTION!!

[MVP Summit 2014] Roslyn Hackathon, Analyzers!!

dotnet_logo

Today with my Italian friends (Raffaele Rialdi aka Raf, Marco Minerva and Renato Marzaro), we’re working on Roslyn, the new open source C# compiler by Microsoft community, in detail today we’re working on creating new analyzers, that helps developer with live feedback while developing, like validating a regular expression, before going live and see the exception at runtime, an analyzer can provide such a feedback while writing code / at compile-time.

Marco Minerva and me, we’re working on a regular expression analyzer, to help find if the literal regex written in code is valid, before run the application, will be just a prototype, and if it will works :) will be push it on github (check our repo, starting from next week).

Stay tuned!! :)

C# .NET – Collections Comparison

generic.net.collection.interfaces

I liked a James Michael Hare’s post “C#/.NET Fundamentals: Choosing the Right Collection Class“, and I like to re-blog the comparison table:

Collection Ordering Contiguous Storage? Direct Access? Lookup Efficiency ManipulateEfficiency Notes
Dictionary Unordered Yes Via Key Key:O(1) O(1) Best for high performance lookups.
SortedDictionary Sorted No Via Key Key: O(log n) O(log n) Compromise of Dictionary speed and ordering, uses binarysearch tree.
SortedList Sorted Yes Via Key Key:O(log n) O(n) Very similar toSortedDictionary, except tree is implemented in an array, so has faster lookup on preloaded data, but slower loads.
List User has precise control over element ordering Yes Via Index Index: O(1)
Value: O(n)
O(n) Best for smaller lists where direct access required and no sorting.
LinkedList User has precise control over element ordering No No Value:O(n) O(1) Best for lists where inserting/deleting in middle is common and no direct access required.
HashSet Unordered Yes Via Key Key:O(1) O(1) Unique unordered collection, like a Dictionary except key and value are same object.
SortedSet Sorted No Via Key Key:O(log n) O(log n) Unique sorted collection, like SortedDictionary except key and value are same object.
Stack LIFO Yes Only Top Top: O(1) O(1)* Essentially same as List except only process as LIFO
Queue FIFO Yes Only Front Front: O(1) O(1) Essentially same as List except only process as FIFO

PASS SQL Saturday Parma Agenda now available!

Originally posted on Alessandro Alpi's Blog:

PASS SQL Saturday Parma agenda is now available!

This is the timetable:
From        To
————————–
10:00       11:00
11:15       12:15
12:30       13:30
13:30       14:30
14:30       15:30
15:45       16:45
16:45       17:00
17:00       18:00
————————–
 
We will have three tracks:
  • BI, for Business Intelligence, BigData, xVelocity, etc.
  • DEV, for developers 
  • OPS, for operations and administration
Checkout the agenda here and organize your Saturday with the schedule builder!
 
Stay Tuned! :)
 
 
 
 

View original

Start all services filtering by Name and StartMode / StartupType using PowerShell

Sometimes I need to start or restart a lot of services belonging to my application, all having the product / company name as prefix, and I would like to start it all, but not the ones that are flagged as Manual.

To simple start all the services that match a prefix you would do something like this:

Start-Service "MyProduct*"

but when you need to start the service that are not flagged as Manual startup, there’s no command/filter directly in powershell command, but you need to use Wmi objects in this way:

Get-WmiObject win32_service | where { $_.Name -like "MyProduct*" -and $_.StartMode -ne "Manual" } | Start-Service

With the same syntax you can of course, do Get-Service, Stop-Service, and all the other related commands.

RabbitMQ: Completely Hard Reset a RabbitMQ Server Installation

clean-rabbitmq

Probably the most exciting thing of this post is the funny image of RabbitMQ’s cleaner :)

Yes, you can also reset the RabbitMQ instance, by running these commands:

rabbitmqctl.bat stop_app

rabbitmqctl.bat reset

rabbitmqctl.bat start_app

soft-reset-rabbitmq

…but sometimes could be necessary to clean up stuff, especially when you have played around cluster, multiple node on the same machine, ad so on.

It took me a while to figure out who was maintaining a reference to the %appdata%\RabbitMQ folder, even after I uninstalled Erlang, so here’s the simple steps:

  1. Uninstall RabbitMQ Server
  2. Kill the process epmd.exe (this was the process maintaning a reference to the RabbitMQ folder)
    rabbitmq-epmd-process
  3. Delete the folder RabbitMQ that is under %appdata%
    rabbitmq-folder-wont-delete If you also want to change the erlang cookie, you will need to delete also the file “.erlang.cookie” that you find under your users folder %userprofile%
    rabbitmq-erlang-cookie If you have a cluster and remove .erlang.cookie the node would no be able to rejoin the cluster
  4. Reinstall RabbitMQ Server

 

That’s all folks!