question

qskzhn-3054 avatar image
0 Votes"
qskzhn-3054 asked JackJJun-MSFT commented

How to create NUnit testing for a method with "switch case"?

I'm brand new in NUnit testing. Here I've a problem. I've a method with "switch case ... default". What is best method to write a unit testing for that method. Here i'm coping the method.

using System;
using System.IO;
using System.Text;

namespace Exercise3
{
public class Exercise2
{
public void ipk()
{
double nim;
string nama, kelas;
string namaFile;
string[] mk = new string[6];
string[] nilHuruf = new string[6];
double[] nilAngka = new double[6];

         try
         {
             Console.Write("Nama File : ");
             namaFile = Convert.ToString(Console.ReadLine());

             Console.WriteLine("=================IPK=================");
             Console.WriteLine("=====================================");
             Console.Write("Masukkan NIM: ");
             nim = Convert.ToDouble(Console.ReadLine());
             Console.Write("Masukkan Nama: ");
             nama = Convert.ToString(Console.ReadLine());
             Console.Write("Masukkan Kelas: ");
             kelas = Convert.ToString(Console.ReadLine());
             Console.WriteLine("\n-------------------------------------");

             for (int i = 0; i < 6; i++)
             {
                 Console.Write("Masukkan Nama Matakuliah: ");
                 mk[i] = Convert.ToString(Console.ReadLine());
                 Console.Write("Masukkan Nilai Angka: ");
                 nilAngka[i] = Convert.ToDouble(Console.ReadLine());
                 Console.Write("Masukkan Nilai Huruf: ");
                 nilHuruf[i] = Convert.ToString(Console.ReadLine());
                 Console.WriteLine();
             }

             StreamWriter sw = new StreamWriter("D:\\UMY\\TI Sem 2\\Pengembangan Aplikasi Windows\\Activity\\Exercise3\\IPK.txt");

             sw.WriteLine("\n-------------------------------------");
             sw.WriteLine("Transkip Kuliah");
             sw.WriteLine("-------------------------------------\n");
             sw.WriteLine("NIM: {0}", nim);
             sw.WriteLine("Nama: {0}", nama);
             sw.WriteLine("Kelas: {0}", kelas);
             sw.WriteLine("-------------------------------------\n");
             sw.WriteLine("Transkip Mahasiswa dengan NIM: {0}", nim);
             sw.WriteLine("-------------------------------------");
             sw.WriteLine("Mata Kuliah \tNilai Angka \tNilai Huruf");
             sw.WriteLine();
             for (int i = 0; i < 6; i++)
             {
                 sw.WriteLine("" + mk[i] + "\t\t" + nilAngka[i] + "\t\t" + nilHuruf[i]);
             }
             sw.Close();
         }
         catch (Exception e)
         {
             Console.WriteLine("Exception : " + e.Message);
         }
         finally
         {
             Console.WriteLine("Executing finally block.");
         }
     }

     public void KartuKeluarga()
     {
         string[] nama, pendidikan, status;
         int input;
         string namaFile;

         try
         {
             Console.Write("Nama File : ");
             namaFile = Convert.ToString(Console.ReadLine());

             Console.WriteLine("============Kartu Keluarga============");
             Console.WriteLine("======================================");
             Console.Write("Masukkan Jumlah Anggota Keluarga: ");

             input = Convert.ToInt32(Console.ReadLine());
             nama = new string[input];
             pendidikan = new string[input];
             status = new string[input];
             for (int i = 0; i < input; i++)
             {
                 Console.Write("Masukkan Nama Lengkap: ");
                 nama[i] = Convert.ToString(Console.ReadLine());
                 Console.Write("Masukkan Pendidikan Terakhir: ");
                 pendidikan[i] = Convert.ToString(Console.ReadLine());
                 Console.Write("Masukkan Status: ");
                 status[i] = Convert.ToString(Console.ReadLine());
                 Console.WriteLine();
             }

             StreamWriter sw = new StreamWriter("D:\\UMY\\TI Sem 2\\Pengembangan Aplikasi Windows\\Activity\\Exercise3\\Kartu Keluarga.txt");

             sw.WriteLine("Data Kartu Keluarga \n-------------------- \nNama Lengkap \tPendidikan Terakhir \tStatus");

             for (int i = 0; i < input; i++)
             {
                 sw.WriteLine("" + nama[i] + "\t\t\t" + pendidikan[i] + "\t\t\t\t" + status[i]);
             }
             sw.Close();
         }
         catch (Exception e)
         {
             Console.WriteLine("Exception : " + e.Message);
         }
         finally
         {
             Console.WriteLine("Executing finally block.");
         }
     }
     static void Main(string[] args)
     {
         Exercise2 ex = new Exercise2();
         string ulang;
         do
         {
             Console.WriteLine("===============Menu Utama===============");
             Console.WriteLine("========================================");
             Console.WriteLine("1. IPK \n2. Kartu Keluarga");
             Console.WriteLine("========================================");
             int pilih;
             Console.Write("Silahkan Pilih Menu: ");
             pilih = Convert.ToInt32(Console.ReadLine());
             switch (pilih)
             {
                 case 1:
                     Console.Clear();
                     ex.ipk();
                     break;
                 case 2:
                     Console.Clear();
                     ex.KartuKeluarga();
                     break;
             }
             Console.Write("Apakah ingin kembali ke menu utama? (Y/N)");
             ulang = Convert.ToString(Console.ReadLine());
             Console.Clear();
         } while (ulang == "Y" || ulang == "y");
     }
 }

}

dotnet-csharpdotnet-cli
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@qskzhn-3054 , Welcome to Microsoft Q&A, Since your problem is related to Nunit, it is a 3rd party product, I recommend that you could ask your question in the following link:
https://github.com/nunit/nunit/issues

Thanks for your understanding.

0 Votes 0 ·

0 Answers