lundi 30 novembre 2015

Test Failed - Failed to set up the execution context to run the test

I am attempting to write unit tests for a set of classes that access or modify global variables.

The first global I needed to wrap up was:

extern std::atomic<Foo> GlobalFoo;

Where Foo is an enum class. I wrote 2 separate classes to handle the setting and getting of the value. I constructed unit tests that passed as expected. Within the test files I include variables to copy the global state (plus extra stateless objects that I may use for testing). I can then use these global copies for restoring values after each test. For example:

namespace Tests
{
    // Global save state
    const Framework::Foo originalGlobal = Framework::GlobalFoo;

    // Test constants
    const auto service = Framework::FooRetriever();
    const auto testFoo = Framework::Foo::SomeValue;

    TEST_CLASS(FooRetrieverUnitTests)
    {

        ...

The next global I started working on was:

extern const std::unordered_map<
    std::vector<char>, std::vector<char>, std::hash<std::vector<char>> GlobalBar;

I want to construct a class that is going to return a shared pointer to this global collection.

I start off by writing my test constants:

namespace Tests
{
    // Alias
    using Collection = const std::unordered_map<
        std::vector<char>, std::vector<char>, std::hash<std::vector<char>>;

    // Global state
    const std::shared_ptr<Collection> global = 
        std::make_shared<Collection>(Framework::GlobalBar);

    TEST_CLASS(FooRetrieverUnitTests)
    {
        // TODO
    };
}

However, running the test at this point produced the following message for all my tests (i.e. not just this new test):

Failed to set up the execution context to run the test

Commenting out the global allows all tests to run.

If I move the line commented as // Global state into a test method then the test will run without problem.

I looked into the error, but could not find anything that applied to my problem. I suspect the error message is masking the real problem. When I run the tests via command line I do not get any extra information. I was led to believe that it was possibly due to a missing DLL (although I found that hard to believe, seeing as I was merely using the standard library with no extras).

My project is a static library, and my unit tests are in the form of a DLL (this is the default approach used by Microsoft Visual Studio's unit testing framework).

Minus the usual "don't use globals" rant, I was wondering if anyone could provide some insight into the problem I am encountering. Perhaps I am doing something wrong by making a copy of the global collection in my Tests namespace or there is another bug in Visual Studio's unit testing feature.

The code compiles without error or warnings but the unit tests will not run in this state.

Aucun commentaire:

Enregistrer un commentaire