Basit Bir Test Framework Gelistirelim 13

2023-05-11

Testlerin birbirinden izole olması önemlidir, çünkü bir testin durumu başka bir testi etkilememelidir. Bunun için, her bir test metodu çalıştırıldığında, test sınıfının yeni bir örneğini oluşturabiliriz. Bu, her bir testin kendi durumunu korumasını sağlar ve testler arasında durum paylaşımını önler.

Aşağıda, bu özelliği eklemek için TestRunner‘ı güncelleyelim:

using System;
using System.Reflection;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

// ...

public static class TestRunner
{
    // ...

    public static async Task RunTestsAsync<T>(string suiteName = null)
    {
        var testMethods = typeof(T)
            .GetMethods()
            .Where(m => m.GetCustomAttributes<TestAttribute>().Any() 
                        && (suiteName == null 
                            || m.GetCustomAttributes<TestSuiteAttribute>().Any(s => s.SuiteName == suiteName)))
            .ToList();

        foreach (var testMethod in testMethods)
        {
            var testClassInstance = Activator.CreateInstance<T>();

            await RunTestMethodAsync(testClassInstance, testMethod);
        }
    }

    // ...
}

Bu kod, her bir test metodu için yeni bir test sınıfı örneği oluşturur. Bu, testlerin birbirlerini etkilememesini sağlar, çünkü her test kendi test sınıfı örneğini kullanır.

Bu özelliği eklemenin bir başka yolu da “Setup” ve “Teardown” metotları eklemektir. “Setup” metodu her test öncesinde, “Teardown” metodu ise her test sonrasında çalıştırılır. Bu metotlar genellikle test sınıfının durumunu başlangıç durumuna getirmek için kullanılır. Bu özellik, testlerin birbirinden izole olmasını sağlar ve test sırası hatalarını önler.



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 