c#


NUnit executes with alternate constructor


I have a class which has some unit tests, but when I am running tests I would like the class to be created with a different constructor. Something like this:
[TestFixture]
public class MyClass
{
public MyClass() { /* set up for production */ }
[TestFixtureConstructor]
public MyClass() { /* set up for testing */ }
[Test]
public void FirstTest()
{
// assert some values
}
}
Is this even possible?
I have considered using a static factory method for creating the class in production (with a private constructor), and a public constructor for the testing framework.
Does anyone have any other solutions?
If you really really want this you can take a look at TestFixtureSetUp.
Here's the introduction:
This attribute is used inside a TestFixture to provide a single set of functions that are performed once prior to executing any of the tests in the fixture. A TestFixture can have only one TestFixtureSetUp method. If more than one is defined the TestFixture will compile successfully but its tests will not run.
Example:
namespace NUnit.Tests
{
using System;
using NUnit.Framework;
[TestFixture]
public class SuccessTests
{
[TestFixtureSetUp] public void Init()
{ /* ... */ }
[TestFixtureTearDown] public void Dispose()
{ /* ... */ }
[Test] public void Add()
{ /* ... */ }
}
}
But you should use this with care, or else it defeats the purpose of unit test.
You don't do this.
You do not have your tests written inside the class that you use in real code; you write your tests external to the classes. I believe most testing suites have the concept of 'Teardown' and the opposite; to set up the test environment for a given execution.
To give a quick example of silky's correct approach:
public class MyClass
{
// ...
}
// In a different assembly:
[TestFixture]
public class TestMyClass
{
[SetUp]
public void SetUp()
{
_myClass = new MyClass();
}
[Test]
public void FooReturnsTrue()
{
Assert.That(_myClass.Foo(), Is.True);
}
// more tests
private MyClass _myClass;
}

Related Links

Define variables inside app.config
Linq to SQL C# Get Products in all child/sub child Categories too slow
C++ DLL does not unload with AppDomain
Sharing DB Access Methods between Client and Server in Entity Framework (C#.Net)
Can a Delphi 4 and/or Delphi 5 executable be integrated into a C# application?
WCF/REST Get image into picturebox?
Sonar-runner Exception with .NET Project
C# List<object> to IList cast bug in Unity3d
Logging lambda expressions
Deserializing response
How to find out size occupied by data table/data set in asp.net C#?
Change DB connection string after publishing a C# console app
excanvas js is not working in IE8 [duplicate]
count number of identical elements in two arrays in linq
WCF logging, set max file size?
LINQ concatenating elements in two string arrays [duplicate]

Categories

HOME
reactjs
excel-formula
model-view-controller
readdir
highmaps
nltk
procmon
country-codes
rmarkdown
webpack-dev-server
wkhtmltopdf
theory
facebook-sharer
chef-recipe
tracking
android-notifications
jinja2
box2d
greensock
taxonomy
loader
draw2d
legend-properties
single-page-application
cherrypy
polybase
serenity-js
jira-zephyr
pydub
orgchart
ipython-parallel
vtd-xml
drawrect
andengine
log4net-configuration
unity3d-5
ilog
voyager
gitosis
outlook-2010
const
area
sknode
paperclip
wufoo
qtwebkit
line-endings
react.rb
history
openpgp.js
dynatable
copy-constructor
timefield
settext
jigsaw
particle-system
git-cherry-pick
gdi
music-player
askbot
borland-c++
vectordrawable
tinymce-plugins
avd
pysvn
jsonmodel
go-gin
sat4j
power-management
data-layers
adabas
svg-android
thucydides
8-puzzle
mobile-robots
grape-api
kendo-window
nsnumber
nokogiri
record-locking
source-depot
select2-rails
gwt2
ncml
quit
hypertable
win-prolog
commonsware
log-shipping
virtual-destructor
arraycollection
text-manipulation
xslt-grouping
ffdshow
asp.net-1.1

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App