Skip to content
  • English Deutsch

benjamin-abt.com

Performance is a feature | Cloud - AI - Azure - .NET - Sustainability

  • Home
  • Blog
  • Portfolio
    • Overview
    • Workshops & Training
    • Publications
  • Contact
  • Home
  • Blog

Unit-Testen von internen Klassen in C#

benjamin-abt.com
September 2, 2014 2 min read BEN ABT
Csharp Testing Unit Tests

Dieser Artikel stammt aus dem Jahr 2014. Versionen, Werkzeuge und Empfehlungen können sich seitdem geändert haben. Neuere Beiträge findest du unter Csharp.

Die Zeit am Flughafen möchte ich dazu nutzen einer meiner Anfragen per E-Mail zu beantworten.

Hallo Benjamin, wie testest du Quellcode, der mit internal deklariert ist?

Ich bin kein Fan von 100%-tiger Testbarkeit von Code; einfach weil es für auch mich eine Frage der Effizient ist. Ich beschränke mich auf ~90%-tiger Testbarkeit – jedenfalls im C#-Bereich. Dennoch finde ich die Frage gar nicht so schlecht, da der internal-Modifier mit dem 100% test code coverage-Ansatz im Konflikt steht: interner Code lässt sich durch externe Text-Projekt nicht testen.

Sofern die konsumierende Testklasse bzw. dessen Projekt feststeht, kann ganz einfach das InternalsVisibleTo-Attribut verwendet werden. Oft gebe ich aber meine Debug- und Release-Stände direkt weiter, sodass ich die konsumierenden Test-Projekte so gar nicht kenne.

Selbst verwende ich gerne interne Klassen, um zum Beispiel Konfigurationsabfragen nicht von außen verfügbar zu machen. Anhand diesem Beispiel möchte ich exemplarisch mein Vorgehen zeigen, das das InternalsVisibleTo-Attribut umgeht bzw. eine Alternative darstellt..

C#
 1#if DEBUG
 2    public 
 3#endif 
 4#if (!DEBUG) // == RELEASE
 5    internal
 6#endif
 7 
 8    static class InternalGlobalConfigSwitches
 9    {
10        /// <summary>
11        /// true if minification is enabled
12        /// </summary>
13        public static Boolean HtmlMinificationEnabled()
14        {
15            return IsEnabled( "SC:WU:HtmlMinifyEnabled" );
16        }
17 
18        /// <summary>
19        /// key is enabled
20        /// </summary>
21        /// <param name="key"></param>
22        public static Boolean IsEnabled( String key )
23        {
24            bool enabled;
25 
26            Boolean.TryParse( ConfigurationManager.AppSettings[ key ], out enabled );
27 
28            return enabled;
29        }
30    }

Durch die #if-Direktive ist meine Klasse im Debug-Modus öffentlich und kann damit für UnitTests problemlos verwendet werden. Im Release- bzw. Nicht-Debug-Modus (was standardmäßig dem Release-Modus entspricht) ist Klasse nur noch intern Verfügbar.

Der dazugehörige Test-Code entspricht einer normalen Unit-Test-Klasse:

C#
1[TestClass( )]
2public class InternalGlobalConfigSwitchesTests
3{
4    [TestMethod( )]
5    public void HtmlMinificationEnabledTest()
6    {
7        Assert.IsTrue( InternalGlobalConfigSwitches.HtmlMinificationEnabled( ) );
8    }
9}

Fertig ist mein Test-Verhalten für interne Klassen.

Related articles

Handle time correctly in .NET: TimeProvider

Jul 27, 2026 - 10 min read

Handle time correctly in .NET: TimeProvider

Time is an external dependency. That may sound unusual at first because DateTime.UtcNow is available anywhere without configuration. This …

Create a Local SQL Server Database File with .NET

Feb 9, 2026 - 4 min read

Create a Local SQL Server Database File with .NET

Local SQL Server database files provide a pragmatic way to keep a real SQL Server engine close to the application without provisioning a …

Containerizing .NET in 2026 Without a Dockerfile

Oct 20, 2026 - 11 min read

Containerizing .NET in 2026 Without a Dockerfile

Most .NET Dockerfiles repeat the same five steps: restore, publish, select the matching Microsoft runtime image, copy the output and start …


BEN ABT

BEN ABT

Ben is Chief Engineering Officer and Principal Software Engineer at Medialesson.de, with a strong focus on .NET and Microsoft Azure. In his professional role, he designs and builds highly scalable platforms for hybrid and cloud-based scenarios. He also advises companies and C-levels on migrating to the sovereign cloud.

He runs myCSharp.de, the largest and most active German-speaking C# community forum, actively contributes to both his own and external open-source projects and regularly shares his knowledge through his blog, at community events, conferences and as a writer for technical magazines.

Let's Work Together

Looking for an experienced Platform Architect or Engineer for your next project? Whether it's cloud migration, platform modernization or building new solutions from scratch - I'm here to help you succeed.

New Platforms

Modernization

Training & Consulting

Contact Me
X Facebook LinkedIn WhatsApp
Back Next Blog Post

Open for work!

Contact Me
BEN ABT BEN ABT Software Engineer · Microsoft MVP Passionate about .NET, Azure, performance optimization and sustainable software engineering.

Medialesson GmbH Logo

Microsoft MVP Logo

Recent posts

View all
Stop Using nginx as Your Default Static File Server in Docker

Stop Using nginx as Your Default Static File Server in …

Oct 24, 2026
Containerizing .NET in 2026 Without a Dockerfile

Containerizing .NET in 2026 Without a Dockerfile

Oct 20, 2026
Problem Details in ASP.NET Core and .NET 11

Problem Details in ASP.NET Core and .NET 11

Oct 5, 2026
Resilience in .NET with Microsoft.Extensions.Resilience

Resilience in .NET with Microsoft.Extensions.Resilience

Sep 21, 2026
What Matters in ASP.NET Core 11

What Matters in ASP.NET Core 11

Sep 14, 2026

Tags

.NET (192) Csharp (96) ASP.NET Core (48) Azure (41) DevOps (17) Docker (14) Microsoft (14) EF Core (11) Microsoft MVP (11) AI (10) GitHub (10) GitHub Copilot (10) ASP.NET (9) Security (9) Synology (9) Architecture (8) Raspberry (8) Azure Functions (7) Cloudflare (7) Bicep (6)
benjamin-abt.com

About BEN

Benjamin Abt is a Software Engineer, Platform Architect and Microsoft MVP specializing in .NET, Azure, cloud architecture, performance optimization and sustainable software development.

Connect

  • Follow me on GitHub
  • Follow me on LinkedIn
  • Follow me on Mastodon
  • Microsoft MVP profile
  • Follow SchwabenCode on GitHub
  • Follow me on X

Quick Links

  • Portfolio
  • Blog
  • Workshops & Training
  • Publications
  • Get in Touch
  • Sitemap
Open for Work

© Copyright Benjamin Abt & SchwabenCode | 2011 - 2026

Content licensed under CC BY 4.0 unless otherwise noted.

  • Impressum
  • Privacy
  • Contact