Basit Bir Test Framework Geliştirelim 11

2023-05-11

Testlerin daha iyi organize edilmesine ve belirli test gruplarının seçilerek çalıştırılabilmesini istesek ne yaparız. Testleri belirli grup veya kategori halinde düzenlesek?

Bu özelliği eklemek için, bir TestSuiteAttribute oluşturabiliriz. Bu attribute, bir test sınıfına veya test metoduna uygulanabilir. Ayrıca, TestRunner‘ı güncelleyerek, belirli bir test grubu için testleri çalıştırabiliriz.

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

// ...

public class TestSuiteAttribute : Attribute
{
    public string SuiteName { get; }

    public TestSuiteAttribute(string suiteName)
    {
        SuiteName = suiteName;
    }
}

public static class TestRunner
{
    // ...

    public static async Task RunTestsAsync<T>(string suiteName = null)
    {
        var testClassInstance = Activator.CreateInstance<T>();

        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)
        {
            // ... önceki kod ...
        }
    }

    // ...
}

TestSuiteAttribute bir test grup adını saklar. TestRunner‘ın RunTestsAsync metodu, bir grup adı alır ve bu gruba ait testleri çalıştırır. Eğer grup adı belirtilmemişse, tüm testler çalıştırılır.

Bir test sınıfı veya metodu, bir veya daha fazla test grubuna ait olabilir. Test grubu belirtmek için, test sınıfına veya metoduna TestSuiteAttribute‘u uygulayabilirsiniz:

[TestSuite("IntegrationTests")]
public class MyIntegrationTests
{
    [Test]
    public async Task Test1()
    {
        // ...
    }

    [Test, TestSuite("DatabaseTests")]
    public async Task Test2()
    {
        // ...
    }
}

Bu örnekte, MyIntegrationTests sınıfı “IntegrationTests” grubuna aittir ve Test2 metodu hem “IntegrationTests” grubuna hem de “DatabaseTests” grubuna aittir. TestRunner.RunTestsAsync("IntegrationTests") çağrıldığında, her iki test de çalıştırılır. Ancak TestRunner.RunTestsAsync("DatabaseTests") çağrıldığında, sadece Test2 çalıştırılır.



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 