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.

9
  • I believe your are seeing this error because you haven't installed Security.Permissions package on your test project. Give it a try Commented Mar 12, 2020 at 12:24
  • Thank you. I did that, unfortunately still get the same exception. Commented Mar 12, 2020 at 13:20
  • Have you added it (System.Security.Permissions) as a reference as well? Commented Mar 12, 2020 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 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. Commented Mar 12, 2020 at 13:41
  • 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. Commented Mar 16, 2020 at 14:17

1 Answer 1

-1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.