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

Customise NewtonSoft.Json for Value Object serialisation [duplicate]
Combobox Column biding in WPF with MVVM pattern (Galasoft)
writing array values to file C#
How do I send data to the pre-generated Asp.net database?
Binding combobox - WPF & C#
Suppress exception while querying documentDB
WPF treeview context menu
add ComboBox dynamically with Locations in UWP
In the result of the analysis of the SONARQUBE server does not show correctly the Total of lines of code and nor the errors of code
How do I refer to a control in a WPF nested ListView?
Not able to read an XML file in a directory?
ASPX Session State null exception on Postback
Selenium and Aurelia, how make test wait for the page to load and HTTP calls to complete
OpenQA.Selenium.ElementNotVisibleException' in WebDriver.dll while trying to click on the link
Add items to Listbox
How do use LParam in Pinvoke GlobalGetAtomName

Categories

HOME
qlikview
jenkins-pipeline
snappy
ibm
c++builder
postsharp
mifare
stdout
azure-resource-manager
bitbucket
grid
ms-access-2010
checkpoint
autocad
google-api-oauth
watson-dialog
comma
google-closure-compiler
reportlab
airbnb
turn.js
dev-c++
onclicklistener
pumping-lemma
ecmascript-2017
datastax-enterprise
ckeditor4.x
rancher
custom-fields
apm
jboss-arquillian
boost-log
jmsserializerbundle
imdb
contextmenustrip
ports
matlab-compiler
powerbi-embedded
border-layout
irr
libzip
nshttpurlresponse
sharpssh
selection-sort
overhead
nsalert
dynatable
qt3d
sqlproj
hdr
xcode7.1
web-optimization
firefox-os
web-performance
newlib
taverna
personality-insights
vspackage
tidesdk
lwuit
nsnotificationcenter
system.net
powerpoint-2010
dotcmis
sendy
connections
grape-api
ia-32
nsnumber
extensibility
asp.net-4.0
pageload
generator-expression
rbm
algol
cryptarithmetic-puzzle
report-viewer2010
expresso-store
hypertable
jquery-ui-map
opendir
qvariant
onunload
arraycollection
miniport
opengl-to-opengles
weak-typing

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