Basit Bir Test Framework Geliştirelim 1

2023-05-11

Unit test yazarken genellikle NUnit veya XUnit gibi mevcut unit test framework’leri kullanılırız. Ancak, eğer basit bir unit test framework’ü kendimiz yazmak istersek, nasıl yaparız?.

Ve başlayalım. Tek bir assert methodumuz ve runner ımızla başlangıcı yapalım.

using System;

namespace SimpleTestFramework
{
    public class TestAttribute : Attribute { }

    public class Assert
    {
        public static void Equal(object expected, object actual)
        {
            if (!expected.Equals(actual))
            {
                throw new Exception($"Test Failed: Expected {expected}, but got {actual}");
            }
        }
    }

    public class TestRunner
    {
        public void RunTests<T>() where T : new()
        {
            var type = typeof(T);
            var instance = new T();

            foreach (var method in type.GetMethods())
            {
                if (Attribute.IsDefined(method, typeof(TestAttribute)))
                {
                    try
                    {
                        method.Invoke(instance, null);
                        Console.WriteLine($"Test Passed: {method.Name}");
                    }
                    catch
                    {
                        Console.WriteLine($"Test Failed: {method.Name}");
                    }
                }
            }
        }
    }
}

Bu framework’ü kullanarak bir test sınıfı aşağıdaki gibi yazılabilir:

using SimpleTestFramework;

public class MathTests
{
    [Test]
    public void TestAddition()
    {
        var result = 2 + 2;
        Assert.AreEqual(4, result);
    }

    [Test]
    public void TestSubtraction()
    {
        var result = 5 - 2;
        Assert.AreEqual(3, result);
    }
}

Ve son olarak, testleri çalıştırabiliriz:

var runner = new TestRunner();
runner.RunTests<MathTests>();

Bu basit framework, başarılı ve başarısız testleri belirler ve sonuçları ekrana basar.



More posts like this

The SemaphoreSlim Class: Multitask-Based Programming in C#

2023-06-11 | #net #semaphoreslim

Overview The SemaphoreSlim class is a structure used in C# to control one or more threads using a specific resource or operation concurrently. SemaphoreSlim limits the number of threads that can access the resource at the same time. The use of SemaphoreSlim is often used to prevent deadlock situations in multi-threaded applications and to ensure that a specific resource is used by only one or more threads at the same time.

Continue reading 


LINQ and Optimistic Concurrency: Methods for Ensuring Data Integrity

2023-06-11 | #linq #net #optimistic-concurrency

When working with databases, it is of great importance to properly manage concurrent operations and ensure data integrity. Concurrency control is used to manage multiple users or processes accessing the same data concurrently. In this article, we will explore how to ensure data integrity using LINQ (Language Integrated Query) with optimistic concurrency. What is Optimistic Concurrency? Optimistic concurrency is a method where resources are not locked when a transaction begins, but changes are checked before the transaction is completed.

Continue reading 


LINQ and Pessimistic Concurrency: Methods for Ensuring Data Integrity

2023-06-11 | #linq #net #pessimistic-concurrency

When working with databases, managing concurrent operations and ensuring data integrity are of great importance. Concurrency control is used to manage multiple users or processes accessing the same data concurrently. In this article, we will explore how to use LINQ (Language Integrated Query) with pessimistic concurrency to maintain data integrity. What is Pessimistic Concurrency? Pessimistic concurrency is based on the principle of locking the relevant data when a transaction begins and maintaining the lock until the transaction is completed.

Continue reading 