Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c#: public when debugging, private otherwise

Is there a nice way to make it so that functions are public when I am testing with NUnit, but private otherwise?

Not having to generate a lot of extraneous code would also be nice.

------------------------Edit------------------------

It seems the solutions fall under 3 types:

  1. don't do what I'm attempting to do.
  2. use compiler directives.
  3. try a clever solution (like using InternalsVisibleTo).

Might there be a way to do this programmatically? i.e. just create a new temporary app that makes all protected/private/internalfunctions public, plug that into NUnit, run the tests there, and then go back to using the private functions for the release version?

like image 611
user420667 Avatar asked Feb 23 '11 18:02

user420667


People also ask

What do you mean by C?

C is a structured, procedural programming language that has been widely used both for operating systems and applications and that has had a wide following in the academic community. Many versions of UNIX-based operating systems are written in C.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Why is C programming language important?

One of the most significant features of C language is its support for dynamic memory management (DMA). It means that you can utilize and manage the size of the data structure in C during runtime. C also provides several predefined functions to work with memory allocation.

Why is C called a mid level programming language?

C has the features of both assembly level languages i.e low-level languages and higher level languages. So that's why C is generally called as a middle-level Language. The user uses C language for writing an operating system and generates menu driven customer billing system.


2 Answers

Instead of private<->public you can make them internal<->public using InternalsVisibleTo.

InternalsVisibleTo is an assembly attribute that you can use in the assembly you want to make visible to your unit test assembly. You will have to sign your unit test assembly, because the InternalsVisibleTo attribute relies on the public key of the calling assembly.

like image 85
Klaus Byskov Pedersen Avatar answered Sep 26 '22 06:09

Klaus Byskov Pedersen


You might not need to if you have good coverage on your public methods which call the private methods but that seems to be another debate into it self.

This is a good link about TDD and different ways to do it: How to Test Private and Protected methods in .NET

like image 27
RussHWalker Avatar answered Sep 26 '22 06:09

RussHWalker