Basit Bir Test Framework Geliştirelim 2

2023-05-11

Basit test framework’ümüze birkaç özellik ekleyelim. SetUp ve TearDown metodları, bu metodlar, her test öncesi ve sonrası çalışır ve testler arasında tekrar eden kodu azaltmaya yardımcı olabilir.

using System;

namespace SimpleTestFramework
{
    public class TestAttribute : Attribute { }
    public class SetUpAttribute : Attribute { }
    public class TearDownAttribute : 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();

            var setUpMethod = Array.Find(type.GetMethods(), method => Attribute.IsDefined(method, typeof(SetUpAttribute)));
            var tearDownMethod = Array.Find(type.GetMethods(), method => Attribute.IsDefined(method, typeof(TearDownAttribute)));

            foreach (var method in type.GetMethods())
            {
                if (Attribute.IsDefined(method, typeof(TestAttribute)))
                {
                    setUpMethod?.Invoke(instance, null);

                    try
                    {
                        method.Invoke(instance, null);
                        Console.WriteLine($"Test Passed: {method.Name}");
                    }
                    catch
                    {
                        Console.WriteLine($"Test Failed: {method.Name}");
                    }

                    tearDownMethod?.Invoke(instance, null);
                }
            }
        }
    }
}

Bu değişikliklerle birlikte, artık testlerimizde SetUp ve TearDown metodları kullanabiliriz:

using SimpleTestFramework;

public class MathTests
{
    private int _baseNumber;

    [SetUp]
    public void SetUp()
    {
        _baseNumber = 2;
    }

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

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

    [TearDown]
    public void TearDown()
    {
        // Clean up any resources used by the test.
    }
}

Bu örnekte, SetUp metodu her testten önce çalıştırılır ve _baseNumber değişkenini 2’ye ayarlar. TearDown metodu ise her testten sonra çalışır ve testlerin kullandığı kaynakları temizleyebilir. Bu özellikler, testlerinizin daha düzenli ve yönetilebilir olmasına yardımcı olabilir.



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 