Can't figure out why my MSTest won't run

Lin 41 Reputation points
2022-10-25T17:36:33.373+00:00

I have a simple program that uses List to store matrixes; this part is used to calculate a number of elements that are more then 3,15. It does what it needs to do but when it comes to testing i'm hit with endless loading with no errors showing.
I'm completely new to this so i need someone to help me out.

namespace MatrixLib  
{  
    public class Matrix  
    {  
        public static List<Matrix> matrixes = new List<Matrix>();  
  
        public int[,] matrix;  
        public int rows;  
        public int columns;  
    }  
  
    public class DoMatrix  
    {  
        public static int Calculate(List<Matrix> matrixes, int indexOne)  
        {  
            int count = 0;  
            for (int i = 0; i < matrixes[indexOne].rows; i++)  
            {  
                for (int j = 0; j < matrixes[indexOne].columns; j++)  
                {  
                    if (matrixes[indexOne].matrix[i, j] > 3.5)  
                        count += 1;  
                }  
            }  
  
            Console.WriteLine("\nМатрица номер " + indexOne + " содержит " + count + " элементов больших 3.5");  
            Console.ReadKey();  
  
            return(count);  
        }  

using Microsoft.VisualStudio.TestTools.UnitTesting;  
using MatrixLib;  
  
namespace MatrixTest  
{  
    [TestClass]  
    public class UnitTest1  
    {  
        [TestMethod]  
        public void TestMethod1()  
        {  
            var expected = 0;  
  
            List<Matrix> matrixes = new List<Matrix>();  
  
            matrixes.Add(new Matrix() { matrix = new int[2, 2] { { 0, 1 }, { 2, 3 } }, rows = 2, columns = 2 });  
  
            var result = DoMatrix.Calculate(matrixes, 0);  
  
            Assert.AreEqual(expected, result);  
        }  
}  
Visual Studio Testing
Visual Studio Testing
Visual Studio: A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.Testing: The act or process of applying tests as a means of analysis or diagnosis.
346 questions
0 comments No comments
{count} votes

Accepted answer
  1. Michael Taylor 53,726 Reputation points
    2022-10-25T19:01:38.757+00:00

    That's because in the middle of your DoMatrix call you have a call to Console.ReadKey. Therefore your code is blocked waiting on a console input it'll never get. Your "library" code should not have any UI calls in it including Console.

    If you need to log data then either use ILogger (for .NET Core code) or System.Diagnostics.Debug to write to the debugger output.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.