DEV Community

Junissen
Junissen

Posted on • Edited on

Smth about Unittests

When we need to improve some path of code, I recommend to write UnitTests.
Programming includes 4 stadies:

  • Math modeling future decision + flowchart
  • Coding each part of decision in a some language
  • Compiling all code with Visual Studio/Online
  • Testing software product + UserTests

using namespace System;
using namespace System::Text;
using namespace System::Collections::Generic;
using namespace Microsoft::VisualStudio::TestTools::UnitTesting;

Each libraries provides path to success in testing and create a good environment for variables.
Namespace constitude an area, where values and functions will be viewed by user for understanding: ok tests/or not.

int power(int a, int b, int n) {// a^b mod n
            int tmp = a;
            int sum = tmp;
            for (int i = 1; i < b; i++) {
                for (int j = 1; j < a; j++) {
                    sum += tmp;
                    if (sum >= n) {
                        sum -= n;
                    }
                }
                tmp = sum;
            }
            return tmp;
        }
Enter fullscreen mode Exit fullscreen mode

Using breakpoints I can watch in output-window temporary result of finishing fuction. Its help to avoid hard analytics work under final output of full programme.

void TestMethod1()
        {
            string message_in, messagge_out;
            vector<pair<int, int> > cypher;

            cout << "Enter your message: " << endl;
            getline(cin, message_in);

            int x = rand() % (593 - 2) + 1;

            crypt(593, 123, x, message_in, cypher);
            decrypt(593, x, cypher, messagge_out);

            String^ str1 = gcnew String(message_in.c_str());
            String^ str2 = gcnew String(messagge_out.c_str());
            Assert::AreEqual(str1, str2, true);
        }
Enter fullscreen mode Exit fullscreen mode

Writing UnitTests makes reading code more loyal to the reviewer in company. I think, not testers, but developers need to write simple Unittests. Becouse only developers knows some special variables and values in programme.

Top comments (0)