Unit tests with Nunit

Asked

Viewed 698 times

2

I just installed in my project the "Nunit" for unit testing.

Installed via Nuget, on the VS2013 console.

I put the notes on top of my methods I went on Explorer test and gave a "Run ALL".

This "Run ALL" is just compiling my project and not showing me which tests

passed and failed.

NOTE: My project was already ready.

My code

using NUnit.Framework;

   [TestFixture]
    public class LogDAO
    {

        /// <summary>
        /// Metodo para inclusão 
        /// </summary>
        /// <param name="UserID"></param>
        /// <param name="Acao"></param>
        /// <returns></returns>
        [Test]
        public void InserirLog(string UserID, string Acao)
        {
            using (entidadesIUS entidades = new entidadesIUS())
            {
                LogUtilizacaoIUS log = new LogUtilizacaoIUS();
                log.UserId = new Guid(UserID);
                log.Acao = Acao;
                log.DataOcorrencia = DateTime.Now;

                entidades.LogUtilizacaoIUS.Add(log);
                entidades.SaveChanges();
            }
        }
    }

My Explorer test only looks like this as I’ve already said.

inserir a descrição da imagem aqui

  • Only an addendum, when you "touch" the database, is no longer a unit test but an integration test. It is less clear that save changes is a "mock". http://artofunittesting.com/definition-of-a-unit-test/

3 answers

4


You must run the test methods in Nunit.

1- Open the Nunit

2- In the menu File click Open Project and select your project’s dll, example Your project.dll. Test methods, marked with [Test].

3- Select the method and click the button Run.

It will indicate in case of success or not.

Exemplo de resultado dos testes

  • Where I bass it Renan?

  • I better wear it anyway.

  • Here http://www.nunit.org/index.php?p=home you can download and find documentation as well.

2

I don’t know him very well Nunit but looking at your code I highlight two things:

1 - You are used the annotation [TesteCase] no parameters, so the method does not receive any values.

2 - You declared the method InserirLog Static

Make these changes:

[TestCase("valor_para _userID","Valor_para_acao")]
public void InserirLog(string UserID, string Acao)
{

Just one more note: You’re not testing anything because I don’t see any Assert

  • want to test only the same method, I switched to Test and removed the Static remains same thing.

  • edited my question.

  • 1

    Do click right click on the method name and look for a type option Run Tests

  • I know that if I click File->> New Project->> Test --> Unit Test Project it will work. But my system is already built and I can’t undo it.

  • Run ALL was supposed to be working.

  • You also installed the Nunit Test Adapter for VS?

  • Yes it already comes in the nuget package.

  • It’s a business system so you can’t put all the code inside this Unit Test Project.

Show 3 more comments

2

Browser other questions tagged

You are not signed in. Login or sign up in order to post.