What are attributes used for in unit testing?

Hello guys, I'm discovering MS unit testing, so far by reading docs I learned about most of the stuff and even set up a few test projects which work just great.

However I can't find any where what is the purpose of Attributes?
for example below attribute defines(or declares?) a pair "attribute name / attribute value" :

1
2
3
4
BEGIN_TEST_METHOD_ATTRIBUTE(testMethodName)
    TEST_METHOD_ATTRIBUTE(attributeName, attributeValue)
    ...
END_TEST_METHOD_ATTRIBUTE()


But what is the purpose of attributes and how can I benefit from implement them in test project?

Here is example shapshot from test project, testing MS Media Foundation component of my audio playlist class
I'm unable how could I use attributes here, for what purpose? if you can provide small sample code explaining attributes that would be great. even if it's Boost test example or some other framework, not MS.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include "pch.h"
#include "playlist.h"
#include "utils.h"
#include "MessageWindow.h"
#include <CppUnitTest.h>
#include <CppUnitTestLogger.h>
#include <CppUnitTestAssert.h>

#define WAIT Sleep(2000);

using namespace MessageWindow;
using namespace Microsoft::VisualStudio::CppUnitTestFramework;

namespace TestPlaylist
{
	Playlist* pPlaylist = nullptr;

	TEST_CLASS(TestPlaylist)
	{
	private:
		static HRESULT hr;
		static HRESULT expected;

	public:
		TEST_CLASS_INITIALIZE(InitClass)
		{
			thread = std::thread(window_thread);
			WAIT

			if (pWindow)
			{
				CHECK_HR(Playlist::CreateInstance(pWindow->GetHandle(), &pPlaylist, true));
				pWindow->SetPlaylist(pPlaylist);

				CHECK_HR(pPlaylist->AddToPlaylist(TEXT("..\\..\\Roulette\\sound\\music\\SuperLite 2500\\04 BGM Appreciare.mp3")));
				CHECK_HR(pPlaylist->AddToPlaylist(TEXT("..\\..\\Roulette\\sound\\music\\vegas casino\\Baca.mp3")));
				CHECK_HR(pPlaylist->AddToPlaylist(TEXT("..\\..\\Roulette\\sound\\music\\vegas casino\\Cong.mp3")));
				CHECK_HR(pPlaylist->AddToPlaylist(TEXT("..\\..\\Roulette\\sound\\music\\vegas casino\\Open.mp3")));
				CHECK_HR(pPlaylist->AddToPlaylist(TEXT("..\\..\\Roulette\\sound\\music\\sega casino\\06 Result.mp3")));
			}
		}

		TEST_METHOD_INITIALIZE(InitMethod)
		{
			CHECK_HR(hr = pPlaylist->ReplayPlaylist());
			WAIT
		}

		TEST_CLASS_CLEANUP(CleanupClass)
		{
			Logger::WriteMessage("Inside CleanupClass");
			CHECK_HR(hr = pPlaylist->Shutdown());
			Assert::AreEqual(expected, hr);
			SafeRelease(&pPlaylist);

			SendMessage(pWindow->GetHandle(), WM_DESTROY, 0, 0);
			thread.join();
		}

		TEST_METHOD(SkipTo)
		{
			Logger::WriteMessage("Inside SkipTo");
			CHECK_HR(hr = pPlaylist->SkipTo(2));
			Assert::AreEqual(expected, hr);
			WAIT
		}
		TEST_METHOD(GetPlaybackTime)
		{
			Logger::WriteMessage("Inside GetPlaybackTime");
			MFTIME presentation, segment;
			CHECK_HR(hr = pPlaylist->GetPlaybackTime(&presentation, &segment));

			MFTIME ms_presentation = MFTimeToMsec(presentation);
			MFTIME ms_segment = MFTimeToMsec(segment);

			MessageBox(nullptr, to_string(ms_presentation).c_str(), TEXT("Info"), MB_OK | MB_ICONINFORMATION);
			MessageBox(nullptr, to_string(ms_segment).c_str(), TEXT("Info"), MB_OK | MB_ICONINFORMATION);
		}
		TEST_METHOD(FastForward)
		{
			Logger::WriteMessage("Inside FastForward");
			BOOL forward;
			pPlaylist->CanFastForward(&forward);

			if (!forward)
			{
				MessageBox(nullptr, TEXT("Can't fast forward"), TEXT("Info"), MB_OK | MB_ICONINFORMATION);
			}
			else
			{
				CHECK_HR(hr = pPlaylist->FastForward());
				WAIT
			}
		}
	};

	HRESULT TestPlaylist::hr = S_OK;
	HRESULT TestPlaylist::expected = S_OK;
}
Last edited on
Topic archived. No new replies allowed.