Visual Studio C++ unit testing (unmanaged) - c++

I'm setting up a unit test suite for some unmanaged C++ code, and I've got some questions.
Setup
I added the project to my solution with Visual C++ → Test → Native Unit Test Project.
Code
In order to figure out how things are grouped in this test framework, and what initialization runs when, I replaced the content of the source file (unittest1.cpp) with a modified version of the USAGE EXAMPLE from the top of CppUnitTest.h:
#include <CppUnitTest.h>
#include <string>
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
using std::string;
BEGIN_TEST_MODULE_ATTRIBUTE()
TEST_MODULE_ATTRIBUTE(L"Date", L"2010/6/12")
END_TEST_MODULE_ATTRIBUTE()
static string Indent("");
void openBlock()
{
Logger::WriteMessage((Indent + "{").c_str());
Indent += " ";
}
void closeBlock()
{
Indent.resize(Indent.size() >= 2 ? (Indent.size() - 2) : 0);
Logger::WriteMessage((Indent + "}").c_str());
}
TEST_MODULE_INITIALIZE(ModuleInitialize)
{
Logger::WriteMessage((Indent + "In Module Initialize").c_str());
openBlock();
}
TEST_MODULE_CLEANUP(ModuleCleanup)
{
closeBlock();
Logger::WriteMessage((Indent + "In Module Cleanup").c_str());
}
TEST_CLASS(Class1)
{
public:
Class1()
{
Logger::WriteMessage((Indent + "In Class1").c_str());
openBlock();
}
~Class1()
{
closeBlock();
Logger::WriteMessage((Indent + "In ~Class1").c_str());
}
TEST_CLASS_INITIALIZE(ClassInitialize)
{
Logger::WriteMessage((Indent + "In Class Initialize").c_str());
openBlock();
}
TEST_CLASS_CLEANUP(ClassCleanup)
{
closeBlock();
Logger::WriteMessage((Indent + "In Class Cleanup").c_str());
}
BEGIN_TEST_METHOD_ATTRIBUTE(Method1)
TEST_OWNER(L"OwnerName")
TEST_PRIORITY(1)
END_TEST_METHOD_ATTRIBUTE()
TEST_METHOD(Method1)
{
Logger::WriteMessage((Indent + "In Method1").c_str());
Assert::AreEqual(0, 0);
}
TEST_METHOD(Method2)
{
Logger::WriteMessage((Indent + "In Method2").c_str());
Assert::Fail(L"Fail");
Logger::WriteMessage("This won't print due to failure above.");
}
};
Output
It generated the output below. (I added some linebreaks for clarity.)
In Module Initialize
{
In Class Initialize
{
In Class1
{
In Method1
}
In ~Class1
In Class1
{
In Method2
}
In ~Class1
}
In Class Cleanup
}
In Module Cleanup
Inferences
From this I infer:
Each TEST_MODULE contains zero or more TEST_CLASSes.
Each TEST_CLASS contains zero or more TEST_METHODs.
TEST_MODULE_INITIALIZE() and TEST_MODULE_CLEANUP() are run once for each TEST_MODULE, before/after its series of TEST_CLASSes are executed.
TEST_CLASS_INITIALIZE() and TEST_CLASS_CLEANUP() are run once for each TEST_CLASS, before/after its series of TEST_METHODs are executed.
Each TEST_METHOD is an instance of a TEST_CLASS. The class constructor is executed immediately before the TEST_METHOD(), and the class destructor is executed immediately after the TEST_METHOD().
Questions
Are my inferences above all correct? Have I missed anything important?
Should/must I always have one and only one TEST_MODULE per source file? Can a TEST_MODULE be split among multiple source files? What is best practice?
May I have more than one TEST_CLASS per file or TEST_MODULE? Are there best practices in this regard?
What is the purpose of ATTRIBUTEs? How are they used?
What is the name of this unit test framework? What search terms can I use to learn more?

Related

[UT][Catch2] Several test cases or one with several sections

Let's say we want to test one file (or class). What should be better approach (always? maybe in special cases?)?
Framework Catch2: https://github.com/catchorg/Catch2
Several test cases
TEST_CASE("First", "[some_details]")
{
Class obj;
REQUIRE(obj.firstFunction() == 1);
}
TEST_CASE("Second", "[some_details]")
{
Class obj;
REQUIRE(obj.secondFunction() == 1);
}
TEST_CASE("Third", "[some_details]")
{
Class obj;
REQUIRE(obj.thirdFunction() == 1);
}
One test case with several sections
TEST_CASE("Check functions", "[some_details]")
{
Class obj;
SECTION("First", "[some_details]")
{
REQUIRE(obj.firstFunction() == 1);
}
SECTION("Second", "[some_details]")
{
REQUIRE(obj.secondFunction() == 1);
}
SECTION("Third", "[some_details]")
{
REQUIRE(obj.thirdFunction() == 1);
}
}
I think we ought to use TEST_CASE for each functionality separately (if it is possible). If it's not then we can use SECTION in case of some dependence. I also think that SECTION are good when we want to check several outputs from one function, for example:
TEST_CASE("Check function first", "[some_details]")
{
Class obj;
SECTION("Positive", "[some_details]")
{
REQUIRE(obj.firstFunction() == 1);
}
SECTION("Negative", "[some_details]")
{
REQUIRE(obj.firstFunction() == 0);
}
}
What's your opinion? Which approach is better (and when)? You got some suggestions?
The main difference is:
Using TEST_CASE you can test each case individually. With SECTION this is not possible in some IDEs (like CLION.

How to make the c ++ application work with the browser [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 11 months ago.
Improve this question
How to make the c ++ application work with the browser. I mean a program that retrieves data from a given page (let's assume that the page displays a string) and then performs some reaction on the page. For example, the page displays a random string, and the program enters the length of the string into the form.
I am a novice programmer, so I care about information and advice on where to start. Thanks in advance for any help.
As I already promised to OP in comments, posting Partial answer, which doesn't answer all questions, but only provides handy tool to wrap (call) any Python code inside C++ program.
In my code snippet I don't even do anything with browsers, but instead show only example of computing Greatest Common Divisor using Python's standard function math.gcd().
I decided to introduce this Python-in-C++ bridge only because there exist many beautiful Python modules that work with browsers or with parsing/composing HTML, hence it is much easier to write such tools in Python instead of C++.
But expert without knowledge of default Python C API, it is not that easy to implement even simple use case - compile text of Python code, pass to it any arguments from C++, receive response arguments, return arguments back to C++. Only these simple actions need usage of a dozen of different Python C API functions. That's why I decided to show how to do it, as I know.
I implemented from scratch (specifically for OP's question) handy class PyRunner which does all the magic, usage of this class is simple:
PyRunner pyrun;
std::string code = R"(
def gcd(a, b):
import math
return math.gcd(a, b)
res = gcd(*arg)
print('GCD of', arg[0], 'and', arg[1], 'is', res, flush = True)
)";
std::cout << pyrun.Run(code, "(2 * 3 * 5, 2 * 3 * 7)") << std::endl;
std::cout << pyrun.Run(code, "(5 * 7 * 11, 5 * 7 * 13)") << std::endl;
Basically you just pass any Python code snippet to PyRunner::Run() method and also any argument (represented as Python object converted to string). Result of this call is also a returned Python object converted to string. You can also use JSON to pass any large argument as string and parse returned argument, as any JSON string is also a valid stringized Python object.
Of course you need a knowledge of Python to be able to write complex code snippets inside C++.
One drawback of my PyRunner class is that for some reason (that I didn't yet understand), you can't import Python module inside global scope, as you can see I did import math within function scope. But this is not a big deal, I think, and maybe some experts will clarify the reason.
To compile and run code you need to have pre-installed Python, and pass Python's include folder and library file as compiler arguments. For example in Windows CLang you do following:
clang.exe -std=c++20 -O3 -Id:/bin/Python39/include/ d:/bin/Python39/libs/python39.lib prog.cpp
and in Linux:
clang -std=c++20 -O3 -I/usr/include/ -lpython3.9 prog.cpp
To run the program either you should provide environment variables PYTHONHOME or PYTHONPATH or run program from Python folder (like d:/bin/Python39/) or do sys.path.append("d:/bin/Python39/") on first lines of Python code snippet embedded in C++. Without these paths Python can't find location of its standard library.
PyRunner class is thread-safe, but only single-threaded always. It means that two calls to .Run() inside two threads will be exclusively blocked by mutex. I use std::mutex instead of Python's GIL to protect from multi-threading, because it is quite alright (and faster), if you don't use Python C API in any other threads simultaneously. Also it is not allowed right now to have two instances of PyRunner objects as it does Py_Initialize() and Py_FinalizeEx() in constructor and destructor, which should be done globally only once. Hence PyRunner should be a singleton.
Below is full C++ code with implementation of PyRunner class and its usage (usage is inside main()). See console output after code below. Click Try it online! link to see compile/run of this code on free GodBolt online Linux servers.
Try it online!
#include <iostream>
#include <functional>
#include <string>
#include <string_view>
#include <stdexcept>
#include <memory>
#include <mutex>
#include <Python.h>
#define ASSERT_MSG(cond, msg) { if (!(cond)) throw std::runtime_error("Assertion (" #cond ") failed at line " + std::to_string(__LINE__) + "! Msg: '" + std::string(msg) + "'."); }
#define ASSERT(cond) ASSERT_MSG(cond, "")
#define PY_ASSERT_MSG(cond, msg) { if (!(cond) || PyErr_Occurred()) { PyErr_Print(); ASSERT_MSG(false && #cond, msg); } }
#define PY_ASSERT(cond) PY_ASSERT_MSG(cond, "")
#define LN { std::cout << "LN " << __LINE__ << std::endl << std::flush; }
class PyRunner {
private:
class PyObj {
public:
PyObj(PyObject * pobj, bool inc_ref = false) : p_(pobj) {
if (inc_ref)
Py_XINCREF(p_);
PY_ASSERT_MSG(p_, "NULL PyObject* passed!");
}
PyObject * Get() { return p_; }
~PyObj() {
Py_XDECREF(p_);
p_ = nullptr;
}
private:
PyObject * p_ = nullptr;
};
public:
PyRunner() {
Py_SetProgramName(L"prog.py");
Py_Initialize();
}
~PyRunner() {
codes_.clear();
Py_FinalizeEx();
}
std::string Run(std::string code, std::string const & arg = "None") {
std::unique_lock<std::mutex> lock(mutex_);
code = StrUnIndent(code);
if (!codes_.count(code))
codes_.insert(std::pair{code, std::make_shared<PyObj>(Py_CompileString(code.c_str(), "script.py", Py_file_input))});
PyObj & compiled = *codes_.at(code);
PyObj globals_arg_mod = PyModule_New("arg"), globals_arg = PyModule_GetDict(globals_arg_mod.Get()), locals_arg = PyDict_New(),
globals_mod = PyModule_New("__main__"), globals = PyModule_GetDict(globals_mod.Get()), locals = PyDict_New();
// py_arg = PyUnicode_FromString(arg.c_str()),
PyObj py_arg = PyRun_String(arg.c_str(), Py_eval_input, globals_arg.Get(), locals_arg.Get());
PY_ASSERT(PyDict_SetItemString(locals.Get(), "arg", py_arg.Get()) == 0);
#if 0
PyObj result = PyEval_EvalCode(compiled.Get(), globals.Get(), locals.Get());
#else
PyObj builtins(PyEval_GetBuiltins(), true), exec(PyDict_GetItemString(builtins.Get(), "exec"), true);
PyObj exec_args = PyTuple_Pack(3, compiled.Get(), globals.Get(), locals.Get());
PyObj result = PyObject_CallObject(exec.Get(), exec_args.Get());
#endif
PyObj res(PyDict_GetItemString(locals.Get(), "res"), true), res_str = PyObject_Str(res.Get());
char const * cres = nullptr;
PY_ASSERT(cres = PyUnicode_AsUTF8(res_str.Get()));
return cres;
}
private:
static std::string StrUnIndent(std::string_view const & s) {
auto lines = StrSplit(s, "\n");
size_t min_off = size_t(-1);
for (auto const & line: lines) {
if (StrTrim(line).empty())
continue;
min_off = std::min<size_t>(min_off, line.find_first_not_of("\t\n\v\f\r "));
}
ASSERT(min_off < 10000ULL);
std::string res;
for (auto const & line: lines)
res += line.substr(std::min<size_t>(min_off, line.size())) + "\n";
return res;
}
static std::string StrTrim(std::string s) {
s.erase(0, s.find_first_not_of("\t\n\v\f\r ")); // left trim
s.erase(s.find_last_not_of("\t\n\v\f\r ") + 1); // right trim
return s;
}
static std::vector<std::string> StrSplit(std::string_view const & s, std::string_view const & delim) {
std::vector<std::string> res;
size_t start = 0;
while (true) {
size_t pos = s.find(delim, start);
if (pos == std::string::npos)
pos = s.size();
res.emplace_back(s.substr(start, pos - start));
if (pos >= s.size())
break;
start = pos + delim.size();
}
return res;
}
private:
std::unordered_map<std::string, std::shared_ptr<PyObj>> codes_;
std::mutex mutex_;
};
int main() {
try {
PyRunner pyrun;
std::string code = R"(
def gcd(a, b):
import math
return math.gcd(a, b)
res = gcd(*arg)
print('GCD of', arg[0], 'and', arg[1], 'is', res, flush = True)
)";
std::cout << pyrun.Run(code, "(2 * 3 * 5, 2 * 3 * 7)") << std::endl;
std::cout << pyrun.Run(code, "(5 * 7 * 11, 5 * 7 * 13)") << std::endl;
return 0;
} catch (std::exception const & ex) {
std::cout << "Exception: " << ex.what() << std::endl;
return -1;
}
}
Console output:
GCD of 30 and 42 is 6
6
GCD of 385 and 455 is 35
35

log4cplus does not show all messages

We are building a module that is part of a full embedded Linux operating systems. We are using log4cplus for diagnostics.
log.properties contains:
log4cplus.logger.CompanyGlue=TRACE, console
cm_bind_helper.h contains much more complicated than this. All of the DBG_XXX messages appear on the console.
#include "qobject.h"
#include "log4cplus/dbglog.h"
class bind_helper : public QObject
{
protected:
const char *debugModuleName = "CompanyGlue";
log4cplus::Logger debugModule { Logger::getInstance(debugModuleName) };
bool set_value(CMPath path, PType &input)
{
auto old_value = CMRoot()->getValue(path);
if (input != old_value) {
DBG_DEBUG("Changing " << path << " to " << input);
}
}
}
cm_bridge.cpp contains much more code than this. However, none of the DBG_XXX messages appear on the console.
#include "cm_bridge.h"
#include "cm_bind_helpers.h"
struct CompanyContentModelBridge::detail_ : public bind_helper
{
std::shared_ptr<company::device_base> base;
detail_(QObject *parent)
: bind_helper(parent)
{
configure_system_advanced_control();
}
void configure_system_advanced_control();
std::string get_exported_file_size();
}
using detail_ = CompanyContentModelBridge::detail_;
void detail_::configure_system_advanced_control()
{
base->add_get_only_entry<std::string>("SPDSZ", get_exported_file_size());
}
std::string detail_::get_exported_file_size()
{
DBG_INFO("get exported file size");
//diag("exported file size");
}
I have tried various ways to configure log4cplus in cm_bridge.cpp, including the same two lines in cm_bind_helpers.h and as follows. I also removed them assuming that inheritance would handle it. Nothing made the DBG_XXX messages appear.
DBG_IMPL_DEBUG_MODULE(CompanyGlue);
Eventually, I added this function to cm_bind_helpers.h. It worked when called from its own class but failed when called from get_exported_file_size() (with comment removed).
void diag(std::string_view what) {
DBG_DEBUG("bind helper diag " << what);
}
I'm stumped. I do not know why messages are being suppressed from one file but not the other.

How to stop MsTest tests execution after *n* failed tests

I want to run unit tests via MS Test (from windows console) in a way that I can stop/kill the test execution whenever the failed tests count exceeds certain threshold value.
For my use case there is no point to keep running tests when certain percentage of the tests already failed.
I can only think in creating a new console app to wrap the mstest.exe execution, so I can parse the standard output in real-time,
and eventually kill the process, for example:
var pi = new ProcessStartInfo()
{
FileName = MS_TEST,
UseShellExecute = false,
RedirectStandardOutput = true,
Arguments = MS_TEST_ARGS
};
int passed = 0; int failed = 0;
using (var process = Process.Start(pi))
{
while (!process.StandardOutput.EndOfStream)
{
string line = process.StandardOutput.ReadLine();
if (line.Contains("Passed"))
passed++;
if (line.Contains("Failed"))
failed++;
if (failed >= THRESHOLD)
{
process.Kill();
break;
}
}
}
Can anyone suggest a better way for doing this? I don't think this is natively supported by MsTest.
PowerShell seems to be an option, but the stdout redirect is not trivial.
Update
As a note, I cannot modify the test code, I need this to be done without modifying the tests code in any way.
Create a BaseTestClass which contains a method responsible for killing the process that runs the tests.
using System.Diagnostics;
namespace UnitTestProject1
{
public class BaseTestClass
{
private readonly int _threshold = 1;
private static int _failedTests;
protected void IncrementFailedTests()
{
if (++_failedTests >= _threshold)
Process.GetCurrentProcess().Kill();
}
}
}
Your must inherit all your test classes from BaseTestClass and use the [TestCleanup] attribute. The TestCleanup() method is evaluated when a test defined in the DemoTests class has finished running. Is in that method where we evaluate the output of the test that has just finished. If it failed, we kill the process responsible for running the tests.
In the following example we have defined three tests. The second test, Test_Substracting_Operation(), is intended to fail intentionally, so the third test will never be run.
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace UnitTestProject1
{
[TestClass]
public class DemoTests : BaseTestClass
{
public TestContext TestContext { get; set; }
[TestCleanup]
public void TestCleanup()
{
if (TestContext.CurrentTestOutcome == UnitTestOutcome.Failed)
{
IncrementFailedTests();
}
}
[TestMethod]
public void Test_Adding_Operation()
{
// Arrange
int x = 1;
int y = 2;
// Act
int result = x + y;
// Assert
Assert.AreEqual(3, result);
}
[TestMethod]
public void Test_Substracting_Operation()
{
// Arrange
int x = 1;
int y = 2;
// Act
int result = x - y;
// Assert
Assert.AreEqual(100, result);
}
[TestMethod]
public void Test_Multiplication_Operation()
{
// Arrange
int x = 1;
int y = 2;
// Act
int result = x * y;
// Assert
Assert.AreEqual(2, result);
}
}
}

Coding State Machine in C++

I'm attempting to code a state machine based on a gumball machine. I have a interface class of a basic state, while I have specific states that use this interface. I have four states, no_quarter, has_quarter, sold, and sold_out states. I also have a gumball machine class that handles these states and depending on which state my machine is in, it will go that class and do the needed action. Here is my code that is having the problem, I'll post my functions also.
Gumball_Machine.h
class Gumball_Machine
{
private:
int gumball_count;
State *current_state;
No_Quarter_State *nqs;
Has_Quarter_State *hqs;
Sold_State *ss;
Sold_Out_State *sos;
public:
Gumball_Machine(int inventory)
{
gumball_count = inventory;
nqs = new No_Quarter_State(this);
hqs = new Has_Quarter_State(this);
ss = new Sold_State(this);
sos = new Sold_Out_State(this);
if (gumball_count == 0)
set_state(sos);
else
set_state(nqs);
}
void insert_quarter()
{
current_state->insert_quarter();
}
void eject_quarter()
{
current_state->eject_quarter();
}
void turn_crank()
{
current_state->turn_crank();
}
void dispense()
{
current_state->dispense();
}
void set_state(State *new_state)
{
current_state = new_state;
}
State *get_no_quarter_state()
{
return nqs;
}
State *get_has_quarter_state()
{
return hqs;
}
State *get_sold_state()
{
return ss;
}
State *get_sold_out_state()
{
return sos;
}
No_Quarter_State.h
#ifndef NO_QUARTER_STATE_H_INCLUDED
#define NO_QUARTER_STATE_H_INCLUDED
#include "State.h"
class No_Quarter_State: public State
{
public:
No_Quarter_State(Gumball_Machine *gbm);
void insert_quarter();
void eject_quarter();
void turn_crank();
void dispense();
};
#endif // NO_QUARTER_STATE_H_INCLUDED
No_Quarter_State.cpp
#include "No_Quarter_State.h"
#include "Gumball_Machine.h"
No_Quarter_State::No_Quarter_State(Gumball_Machine *machine)
{
machine = machine;
}
void No_Quarter_State::insert_quarter()
{
cout << "You inserted a quarter.\n";
machine->set_state(machine->get_has_quarter_state());
}
void No_Quarter_State::eject_quarter()
{
cout << "You must insert a quarter before you can eject one.\n";
}
void No_Quarter_State::turn_crank()
{
cout << "You must insert a quarter before you can crank the handle.\n";
}
void No_Quarter_State::dispense()
{
cout << "You need to pay first before you can get a gumball.\n";
}
The line I'm having an issue is in the No_Quarter_State.cpp
machine->set_state(machine->get_has_quarter_state());
This is giving me a run-time error. I've seen examples like this but I'm not completely sure if this is legal in C++. I'm attempting to switch the state of my gumball machine object.
The error I get is a generic not responding error: "test.ext has stopped working". I'm using CodeBlocks to code this.
In the constructor, the presumed member variable machine is hidden by the parameter.
No_Quarter_State::No_Quarter_State(Gumball_Machine *machine)
{
machine = machine;
}
You can fix this by using initializer list syntax instead:Thanks Sneftel and NathanOliver
No_Quarter_State::No_Quarter_State(Gumball_Machine *machine)
: machine(machine)
{
}
However, in regular method functions, you would have to use this-> if you named the method parameter the same as the member variable. A typical style used to avoid that issue is to prepend m_ or append _ to member names.