[C# 단위테스트] xUnit FlunetAssertions 이용하여 문자형, 불리안 테스트 하기

소개

  • 안녕하세요. 오늘은 xUnit FluentAssertions 이용하여 문자형, 불리한 타입을 테스트 하는 방법에 대해서 알려 드리려고 합니다.
  • xUnit 테스트 진행 및 FluentAssertions 누겟 설치하는 방법은 아래 참조를 통해 링크 걸오 놓도록 하겠습니다.
  • 단위테스트를 처음 접하시는 분들은 설정하시는 방법을 먼저 보고 오시는 것을 추천 드리도록 하겠습니다.
  • 그럼 xUnit에서 문자형과 불리한 타입을 어떻게 테스트 진행하는지 예제 코드를 통해서 보여 드리도록 하겠습니다.


참조



1. 테스트 기본이 되는 코드 작성

  • xUnit 으로 단위테스트를 진행하기 위해서는 단위테스트를 진행할 기본 클래스 및 메서드가 있어야 합니다.
  • 이해하기 쉽게, Calculate 클래스 를 선언하고 Add 메서드를 작성하여 테스트 진행하도록 하겠습니다.
using System;

namespace xUnitProgram
{
    public class Calculate
    {
        public bool AddBollean(int num1, int num2)
        {
            int sum = num1 + num2;

            if (sum == num1 + num2)
            {
                return true;
            }

            return false;
        }

        public string AddString(int num1, int num2)
        {
            return $"The result is {num1 + num2}.";
        }
    }

    class Program
    {
        static void Main(string[] args)
        {

        }
    }
}


2. 단위테스트 진행할 테스트 코드 작성하기

  • 앞서 단위테스트 진행할 기본 베이스 코드를 작성하였습니다.
  • 이제 xUnit 테스트 프로젝트를 생성 후, 불리안, 문자형 타입의 메서드들을 단위테스트 진행해 보겠습니다.
  • 참고로 단위테스트 대상이 되는 메서드는 반드시 public 형식이어야 합니다.
using FluentAssertions;
using Xunit;
using xUnitProgram;

namespace Number_Test
{
    public class UnitTest1
    {
        [Fact]
        public void Should_Assertion_Boolean()
        {
            // Arrange
            Calculate calc = new Calculate();

            // Act
            bool result = calc.AddBollean(2, 3);

            // Assertion
            result.Should().BeTrue();
            result.Should().Be(true);
        }

        [Fact]
        public void Should_Assertion_String()
        {
            // Arrange
            Calculate calc = new Calculate();

            // Act
            string result = calc.AddString(1, 6);

            // Assertion

            // Null 확인
            result.Should().NotBeNull();

            // 빈 문자열 확인
            result.Should().NotBeEmpty();
            result.Should().NotBeNullOrWhiteSpace();

            // == 확인
            result.Should().Be("The result is 7.");
            result.Should().NotBe("The result is 10.");

            // 대소문자 무시 == 확인
            result.Should().BeEquivalentTo("THE RESULT IS 7.");

            // 포함 유무 확인
            // 대소문자 구분해서 포함 유무 확인
            result.Should().Contain("is");
            result.Should().NotContain("IS");


            // 대소문 무시해서 포함 유/무 확인
            result.Should().ContainEquivalentOf("IS");
            result.Should().NotContainEquivalentOf("are");

            // 시작 유/무 확인
            // 대소문 구분해서 시작 유/무 확인
            result.Should().StartWith("The");
            result.Should().NotStartWith("This");

            // 대소문 무시해서 시작 유/무 확인
            result.Should().StartWithEquivalentOf("the");
            result.Should().NotStartWithEquivalentOf("this");

            // 끝 유/무 확인
            // 대소문 구분해서 끝 유/무 확인
            result.Should().EndWith("is 7.");
            result.Should().NotEndWith("is 10.");

            // 대소문 무시해서 끝 유/무 확인
            result.Should().EndWithEquivalentOf("is 7.");
            result.Should().NotEndWithEquivalentOf("is 10.");
        }
    }
}

실행 결과

  • 단위테스트 진행 결과 불리안, 문자형 메서드 모두 테스트를 정상적으로 통과하였습니다.

728x90

이 글을 공유하기

댓글

Designed by JB FACTORY