473,325 Members | 2,771 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Initialising when an Assembly loads

I want to perform some initialisation when an assembly is first loaded.

I know about static constructors, but that's not what I want. A class's
static constructor is not called until something uses the class at run
time. That may be too late.

What I'm actually trying to do is hook up a TraceListener so that calls
to Debug.Assert will throw an exception inside NUnit unit tests.
Because the DefaultTraceListener just pops up a dialog within the NUnit
GUI (which interrupts the unit tests), or else writes to the output
when run without a GUI (e.g. within the TestDriven.NET add-in), the
unit test in which the assertion failed is wrongly flagged as having
succeeded. NUnit needs an exception. I intend to supply that exception
by writing a TraceListener that will be hooked up by my unit tests. I
could hook it up in, say, the SetUp of my unit tests, or in their
static constructor, but what if I forget to do so for one of my test
fixture classes? I would rather do it once and for all on loading the
assembly that contains my unit tests.

But I can't figure out how!

Thanks,
Peter Gummer
Nov 17 '05 #1
8 1908
How about configuring your listener in app.config?

Nov 17 '05 #2
Truong Hong Thi wrote:
How about configuring your listener in app.config?


Nice idea, except that it's not my application that I want to do this
to, it's my applications unit tests. They are executed by some other
app, e.g. the NUnit GUI app.

I guess I could edit the NUnit GUI's config. But most of the time I use
the TestDriven.NET Visual Studio add-in. I wonder what config file I'd
have to edit to make that use my listener.

I think the ideal solution would be if there were an answer to my
original question, i.e. by performing the initialisation on loading the
unit test assembly.

- Peter Gummer
Nov 17 '05 #3
Hi Peter,

How about AppDomain.AssemblyLoad ?

Stephen.

"Peter Gummer" <pe******************@hotnospammail.com> wrote in message
news:ON**************@TK2MSFTNGP10.phx.gbl...
....

What I'm actually trying to do is hook up a TraceListener so that calls
to Debug.Assert will throw an exception inside NUnit unit tests.
Because the DefaultTraceListener just pops up a dialog within the NUnit
GUI (which interrupts the unit tests), or else writes to the output
when run without a GUI (e.g. within the TestDriven.NET add-in), the
unit test in which the assertion failed is wrongly flagged as having
succeeded. NUnit needs an exception. I intend to supply that exception
by writing a TraceListener that will be hooked up by my unit tests. I
could hook it up in, say, the SetUp of my unit tests, or in their
static constructor, but what if I forget to do so for one of my test
fixture classes? I would rather do it once and for all on loading the
assembly that contains my unit tests.

But I can't figure out how!

Thanks,
Peter Gummer

Nov 17 '05 #4
Stephen Ahn wrote:
How about AppDomain.AssemblyLoad ?


Hi Stephen!

Yes, it would be good to set up my TraceListener when that event is
fired. But where would I set up my AssemblyLoadEventHandler? It's the
same problem as for my TraceListener.

I have no "Main". Well, the NUnit GUI has a Main, but I don't want to
go editing that. Anyway, doing so still wouldn't solve the problem for
other things that might run my unit tests, such as TestDriven.NET.

What I really need is something comparable to Delphi's unit
"initialization" section: a decentralised bit of code that is
guaranteed to be run on load. C# seems to have no such thing.

There's no magic way to do this with AssemblyInfo.cs, is there?

-- Peter Gummer
Nov 17 '05 #5

Peter,

Hmm, somehow i didn't think the solution would be that easy (otherwise one
of you guys would have worked it out already :)
Good luck, i hope everyone is doing well over there !

Stephen
"Peter Gummer" <pe******************@hotnospammail.com> wrote in message
news:OH***************@TK2MSFTNGP10.phx.gbl...
Stephen Ahn wrote:
How about AppDomain.AssemblyLoad ?


Hi Stephen!

Yes, it would be good to set up my TraceListener when that event is
fired. But where would I set up my AssemblyLoadEventHandler? It's the
same problem as for my TraceListener.

I have no "Main". Well, the NUnit GUI has a Main, but I don't want to
go editing that. Anyway, doing so still wouldn't solve the problem for
other things that might run my unit tests, such as TestDriven.NET.

What I really need is something comparable to Delphi's unit
"initialization" section: a decentralised bit of code that is
guaranteed to be run on load. C# seems to have no such thing.

There's no magic way to do this with AssemblyInfo.cs, is there?

-- Peter Gummer

Nov 17 '05 #6
Hi Peter,

I think Debug.Assert is more suitable for debugging than for unit
testing purpose.
So I think you don't need to try to convert a Debug.Assert into an
exception. If the exception makes sense, why not throw it instead of
using Debug.Assert?

I think if you wrote complete unit test suite which cover the cases
that Debug.Assert is used, then such an exception cannot escape.

I think unit test should yield the same results when run with either
debug or release builds.

Regards,
Thi - http://thith.blogspot.com

Nov 17 '05 #7
Truong Hong Thi wrote:
So I think you don't need to try to convert a Debug.Assert into an
exception. If the exception makes sense, why not throw it instead of
using Debug.Assert?


Because I'm using Debug.Assert to detect programming errors, in the
manner of Design by Contract. Hopefully these errors will be eradicated
before releasing the product. Debug.Assert has no runtime overhead for
release builds.

So, in a debug build, the failure of a Debug.Assert is a bug. If it
happens to fail in a unit test then I want to know about it. NUnit
detects failures via exceptions. I don't think there's any way to
configure NUnit to detect Debug.Assert failures too. It might be nice
to allow it to keep running, as it would in a release build, as long as
NUnit can detect that it failed and report as much, instead of wrongly
saying the test passed. Doing that would require a hook into NUnit that
I think NUnit just doesn't have.

Regardless of whether I throw an exception or somehow find a way to
tell NUnit that it failed without throwing an exception, I still need
to set up my own TraceListener.

So my original question remains. Is there a way for C# to let me hook
up a TraceListener on assembly load? A static constructor nearly does
what I want, but it may be too late; and I don't have a Main routine
that I can depend on.

Anyway, I've posted a message to the NUnit forum on SourceForge, so I
may get a solution there.

-- Peter Gummer
Nov 17 '05 #8
OK, Peter, do whatever you think is right.
I guess you could write a base class for all of your test fixture, and
put your initialization in either setup method or constructor. To
prevent your listener being added multiple times, you might need to
check for its existence before adding.
Regards,
Thi

Nov 17 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Benjamin | last post by:
Hi, I'm currently writing a Web Services that interacts with a database. To allow me to use not just one database provider (for example, I could use MS Access, SQL Server or MySQL), the Web...
3
by: Daniel | last post by:
how do i access methods when the .net assembly was loaded with late biding? class Class1 { public static void Main() { System.Reflection.Assembly SampleAssembly; SampleAssembly =...
0
by: Ricardo Lopez | last post by:
When I load a Visual C# project which has assembly references (dll references) that do not exist, I get a little yellow triangle with an exclamation point. But the project still loads. If I do...
3
by: Clift Norris | last post by:
I've created a managed C++ assembly containing a managed class that is instantiated from ASP.NET. The managed C++ assembly needs msvcp71.dll and msvcr71.dll. When ASP.NET loads the assembly, it...
4
by: BrianS | last post by:
What is the best strategy for dynamic loading private assemblies in asp.net? I understand, and have confirmed, that any dll placed in the app's /bin dir will get loaded on startup. This is not...
9
by: Gravy | last post by:
Hi I want to use one of my c# assemblies on the client browser (IE), say using javascript to call it. I have seen loads of people asking how they can host a WinForms control in IE but all I...
4
by: steve | last post by:
Hi All After writing numerous programs in VB.net 2005 I find that the Toolbox takes forever to initialise. When it appears it has loads of extra tools mainly report viewer Table Adaptors etc...
1
by: Boot2TheHead | last post by:
http://msdn2.microsoft.com/en-us/library/system.reflection.assembly.reflectiononlyloadfrom.aspx When I open an assembly using Assembly.ReflectionOnlyLoadFrom it loads fine, but when I try to...
8
by: Joe Withawk | last post by:
I have a solution consisting of a c# project as win application and a c++ project as classlibrary. Both are .net 2.0 The classlibrary handles some loading of quicktime movies, but that should not...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.