2

I'm currently trying to work myself into unit testing for my firm, who before have never worked with that conecpt. I've previously written a program that accepts customer IDs and reads the corresponding data (name of the firm, address, etc.) from our iDB2 database and displays it in the UI. Here I want to implement a unit test that, if I input a certain customer ID, expects a certain customer name as output.

The test unfortunately fails, even though it shouldn't. When I try debugging the test it throws the following exception:

System.IO.FileNotFoundException: Could not load file or assembly 'System.Security.Permissions, Version=0.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. Das System kann die angegebene Datei nicht finden.

The unit test looks as follows:

using Kundeninformationen;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace KDNAbrufTest
{
[TestClass]
public class UnitTest1
{
    int input = **customerID**;

    public int Aufruf(int input)
    {
        Methoden.AufrufKundenname(input);
        return input;
    }

    [TestMethod]
    public void TestMethod1()
    {
        string expected = "**customer name**";

        string result = Methoden.AufrufKundenname(input);

        Assert.AreEqual(expected, result);
    }
}
}

The method AufrufKundenname connects to the database in another class by using an iDB2Connection, iDB2Command and iDB2DataAdapter. In the program itself there is no problem with connection to the database but I might have overlooked something I would have needed to implement in my test class.

I've gone through a lot of posts of people having similar problems being solved by adding an AppConfig file to the test folder but unfortunately that didn't solve it for me.

I myself am neither expert of unit testing nor an advanced C# programmer nor is English my first language so please excuse any unclear phrasing and beginner's mistakes.

katobreak
  • 21
  • 3
  • I believe your are seeing this error because you haven't installed Security.Permissions package on your test project. Give it a try – vasilisdmr Mar 12 '20 at 12:24
  • Thank you. I did that, unfortunately still get the same exception. – katobreak Mar 12 '20 at 13:20
  • Have you added it (System.Security.Permissions) as a reference as well? – vasilisdmr Mar 12 '20 at 13:23
  • 1
    Are you compiling for .NETFramework or .NETCoreApp? The first will copy the Security.Permissions.dll from the nuget package into the output folder of your unit test assembly, the latter will not. In the second case you will have to copy that dll into your output folder.see https://stackoverflow.com/q/59284307/9595496 . I'm working on a nuget assembly resolver that is about 98% done and will be released in the coming weeks depending on how much time i find. – MikeLimaSierra Mar 12 '20 at 13:41
  • @vasilisdmr I've installed the nuget package System.Security.Permissions and then added the using directive, is that what you mean? I actually bothed the first on my first try, now that I've properly done both I got a new exception: System.IO.FileNotFoundException: System.Threading.AccessControl, Version=0.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a – katobreak Mar 12 '20 at 14:55
  • @MikeLimaSierra I'm using .NETFramework but thank you for the info, that's good to know! – katobreak Mar 12 '20 at 14:58
  • right click on your testing project and click Add -> Reference. – vasilisdmr Mar 12 '20 at 15:04
  • okay done that, but it doesn't show when I search for it. I've double checked whether I installed it and yes I did, but I can't add a reference – katobreak Mar 12 '20 at 15:17
  • 1
    The Unit Test as a general concept is not right. You are making all the mistakes of bad practices in few lines. I suggest you to learn the fundamentals about UT before moving on. If you keep going this way you will end up with useless tests that don't prove anything and nobody can run but you. – Maximiliano Rios Mar 16 '20 at 14:17

1 Answers1

0

Its seems integration test, not unit test. If you write your test in this way, the problem will occur when a customer you use in the test is deleted from the database. Even if you are deploying it with CI / CD, you have a bigger problem.

If you don't have to, I recommend not writing tests like this.

Isolate your tests from database with mocking.

Emre HIZLI
  • 513
  • 6
  • 14