Testmex => tab (snippet)
In questo periodo sto scrivendo test in continuazione, un po’ perchè sto leggendo il libro di Roy Osherove “The Art of Unit Test”, ed un po’ perchè sto cercando di colmare un gap su dexter. Chi mi frequenta pensa che ormai sono vittima del testing in quanto non faccio altro che parlare di unit test, di come scrivere test, etc., e devo ammettere che un po’ è anche vero :).
Il tutto è partito da una certa persona (un po’ contabile ed un po’ commercialista :D) che mi ha spronato più e più volte a guardare lo sviluppo anche da una prospettiva differente, ossia da quella del testing...per questo non posso che ringraziarlo, anche se per assimilare bene i concetti e metterli in pratica ho impiegato un po’ di tempo, ma credo che sia del tutto normale.
Parlando dell’aspetto pragmatico dei test scritti in questi giorni, posso dire che hanno una cosa che li contraddistingue, ossia la presenza di SharpTestEx e RhinoMock; di fatto mi sono creato uno snippet che mi creasse a sua volta un metodo con la struttura secondo la mia nomenclatura preferita e, nel caso mi aspettassi un’eccezione dal test, mi implementasse anche il controllo della stessa.
Per farla breve tutti i miei test devono avere un nome leggibilissimo, che rispecchi il più possibile i tre aspetti base, quindi far capire cosa si sta testando, con quali valori e cosa ci si aspetta:
“MethodUnderTest_Scenario_ExpectedBehavior”
In un esempio pratico in cui si voglia testare un metodo “GetList”, passando un valore negativo al parametro “pageSize” e aspettandosi dal metodo da testare un’eccezione, il nome del test dovrebbe essere una cosa tipo: “GetList_WithNegativePageSize_ShouldThrowArgumentOutOfRangeException” che, tradotto in soldoni, dovrebbe essere implementato più o meno così:
[TestMethod]
public void GetList_WithNegativePageIndex_ShouldThrowNewArgumentOutOfRangeException()
{
//TODO:Arrage
//TODO:Act
//TODO:Assert
ActionAssert.Throws<ArgumentOutOfRangeException>(() => something).ParamName.Should().Be.EqualTo("pageSize");
}
Purtroppo anche con il copia/incolla può essere scomodo ripetere ogni volta questo codice, così mi sono deciso a scrivere uno snippet che, digitando !testmex + tab!, mi crea automaticamente lo scheletro.
Di seguito lo snippet che possiamo copiare ed incollare direttamente nell’apposita folder
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
<Title>Test Method With Exception Management</Title>
<Shortcut>testmex</Shortcut>
<Description>Code snippet for a test method with Exception </Description>
<Author>Ugo Lattanzi</Author>
</Header>
<Snippet>
<Imports>
<Import>
<Namespace>SharpTestsEx</Namespace>
</Import>
<Import>
<Namespace>Rhino.Mocks</Namespace>
</Import>
</Imports>
<References>
<Reference>
<Assembly>SharpTestsEx.MSTest.dll</Assembly>
<Assembly>Rhino.Mocks.dll</Assembly>
</Reference>
</References>
<Declarations>
<Literal>
<ID>MethodName</ID>
<ToolTip>Replace with the name of the test method</ToolTip>
<Default>MethodName</Default>
</Literal>
<Literal>
<ID>StateUnderTest</ID>
<ToolTip>Replace with the state under test name</ToolTip>
<Default>StateUnderTest</Default>
</Literal>
<Literal>
<ID>ExpectedParameterName</ID>
<ToolTip>Replace with the expected exception parameter name</ToolTip>
<Default>ExpectedParameterName</Default>
</Literal>
<Literal>
<ID>ExceptionType</ID>
<ToolTip>Exception type</ToolTip>
<Function>SimpleTypeName(global::System.Exception)</Function>
</Literal>
</Declarations>
<Code Language="csharp">
<![CDATA[[TestMethod]
public void $MethodName$_$StateUnderTest$_ShouldThrowNew$ExceptionType$()
{
//TODO:Arrage
//TODO:Act
//TODO:Assert
ActionAssert.Throws<$ExceptionType$> ( () => something ).ParamName.Should().Be.EqualTo ( "$ExpectedParameterName$" );
}]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
enjoy the snippet!
Ciauz
The comments for this post are closed.
- There is no TrackBack for this post.
#1 da RoBYCoNTe Tuesday May 2010 alle 01:02
Ottima idea quella dello snippet! Anch'io come te ossessionato da "unit tests" per le apps, alla fine noterai meglio di me che si rivelano utilissimi quando qualcosa cambia e vuoi verificare il funzionamento di un intero ecosistema software (mi piace chiamarlo cosi :°D), complimenti per il blog e per le ottime informazioni che fornisci!
#2 da Ugo Lattanzi Tuesday May 2010 alle 02:02
Ciao Roby,
indubbiamente il test è diventato un po' un'ossessione ed il post di domani (se riesco) sarà orientato ancora al testing.
Ciao e grazie per i complimenti.
#3 da RoBYCoNTe Tuesday May 2010 alle 02:22
Ed aggiungo "indubbiamentedomani sarò qui a leggerlo" , questo blog è stato inserito nella sezione best-practices dei miei preferiti :°D
Buon lavoro Ugo! :)