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

¿How can I change the font-size (original) of AutoScale to correct the low resolution in the .exe program file execution?
Replace a TextBox based on DropDownList choice
Total revenue between two dates not working c# ms access database
Opening WPF Window from another Window
Self Hosted WCF Service throws Error Message “405 Method not allowed”
How to use group by with several joins
How to use group by and min statment of linq for one array?
How to filter a nested relationship
Are tags like asp:xxx the best way to implement data in an ASP.NETapplication?
Deleting a single registry value in C#
C# Replace a text in string with if the text contains in String list
ASP.Net C# Object Persists after Saving to Session Once
Web API - Detecting Client Domain
Stackexchange Redis - How to get large number of records paged
bootstrap select dropdown loading issue
Setting ConverterCulture using x:Static

Categories

HOME
pact
skype-for-business
gcc
model-view-controller
kendo-ui-angular2
qemu
ubuntu-14.04
jshell
uml-designer
install
rtc
ios-universal-links
angular-universal
ng2-charts
camunda
ibatis
orange
audio-streaming
spring-webflow
android-wifi
webgl-earth
git-branch
ejb-3.1
multipart
aws-iot
oracle-golden-gate
symlink
pydub
cloudformation
mex
osgi-bundle
delphi-2010
edirectory
arp
keil
rtos
flickr
jsonapi-resources
http-proxy
gitosis
device-driver
strategy-pattern
console.readline
armadillo
cowboy
observer-pattern
jquery-ui-slider
contenteditable
paperclip
f#-fake
gridgain
spring-ide
confluent
standard-error
jcifs
ajv
indoor-positioning-system
nimbus
avi
supervised-learning
razorengine
jmh
nd4j
markerspiderfier
wonderware
libgcrypt
random-sample
lytro
uinavigationitem
vensim
esb-toolkit-2.1
tidesdk
gmaps4jsf
asp.net-2.0
service-broker
chunks
backtrace
touch-typing
react-os
go-gin
powerpoint-2010
visual-c#-express-2010
isl
node-mongodb-native
acrofields
mdp
mysql-error-1146
surfaceview
randomaccessfile
ie-developer-tools
objectdatasource
extensibility
dd4t
http-compression
microsoft-speech-platform
m3u
openafs
big-endian
jquery-ui-map
jmesa
idtabs
log-shipping
parallel-python
recess
digg
sessiontracking
conditional-operator
nyromodal
nstoolbar
dgml
user-preferences
expander
cstring
office-2003
memory-consumption

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