E D R , A S I H C RSS

Gui Testing With Mfc

CppUnit ์„ ์ด์šฉํ•œ MFC ์—์„œ์˜ GuiTesting (์‹œ๋„์ค‘. ๊ฒฐ๊ณผ๋Š”?)

Dialog Based ์—์„œ์˜ ํ…Œ์ŠคํŠธ

Dialog Based ์˜ ๊ฒฝ์šฐ Modal Dialog ๋ฅผ ์ด์šฉํ•˜๊ฒŒ ๋œ๋‹ค. ์ด ๊ฒฝ์šฐ Dialog ๋‚ด์—์„œ๋งŒ ๋ฉ”์„ธ์ง€๋ฃจํ”„๊ฐ€ ์ž‘๋™ํ•˜๊ฒŒ ๋˜๋ฏ€๋กœ, DoModal ํ•จ์ˆ˜๋กœ ๋‹ค์ด์–ผ๋กœ๊ทธ๋ฅผ ๋„์šด ์ดํ›„์˜ ์ฝ”๋“œ๋Š” ํ•ด๋‹น Dialog ๊ฐ€ ๋‹ซํžˆ๊ธฐ ์ „๊นŒ์ง€๋Š” ์‹คํ–‰๋˜์ง€ ์•Š๋Š”๋‹ค. ๊ณ ๋กœ, CppUnit ์—์„œ์˜ fixture ๋ฅผ ๋ฏธ๋ฆฌ ๊ตฌ์„ฑํ•˜์—ฌ ์“ธ ์ˆ˜ ์—†๋‹ค.

๊ทธ๋ž˜์„œ, ํ…Œ์ŠคํŠธ๋ฅผ ์‹œ๋„ํ• ๋•Œ Modaless Dialog ๋กœ ๋งŒ๋“ค๊ณ  ์‹คํ—˜์„ ํ•˜์˜€๋‹ค.


์›ํ•˜๋Š” ์ž‘๋™ ๋ชจ์Šต์€ ์ด๋ ‡๋‹ค.
  1. Editbox ์— ์•„๋ฌด ๊ธ€์„ ๋„ฃ๊ณ 
  2. Add ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด
  3. List box ์— Editbox ์— ์“ด ๊ธ€์ด ์ˆœ์„œ๋Œ€๋กœ ์ฒ˜์Œ๋ถ€ํ„ฐ ์ฑ„์›Œ์ง€๊ณ 
  4. List box ์—์„œ์˜ ์ปค์„œ๋Š” ์ฑ„์›Œ์ง„ ๊ธ€์— ์œ„์น˜ํ•œ๋‹ค.
  5. List box ์— ๊ฐ’์ด ์ฑ„์›Œ์ง€๊ณ  ๋‚œ ๋’ค, Editbox ์˜ ๊ธ€์€ ์ง€์›Œ์ง„๋‹ค.

1. GUI Runner Setting

์ด๋Š” App ํด๋ž˜์Šค์˜ InitInstance ํ•จ์ˆ˜์—์„œ ํ•ด์ค€๋‹ค.

~cpp 
#include "stdafx.h"
#include "GuiTestingOne.h"
#include "GuiTestingOneDlg.h"

#include "cppunit\ui\mfc\TestRunner.h"

#include "GuiTestCase.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

BEGIN_MESSAGE_MAP(CGuiTestingOneApp, CWinApp)
	//{{AFX_MSG_MAP(CGuiTestingOneApp)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG
	ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()

CGuiTestingOneApp::CGuiTestingOneApp()
{
}

CGuiTestingOneApp theApp;

BOOL CGuiTestingOneApp::InitInstance()
{
	AfxEnableControlContainer();

	CppUnit::MfcUi::TestRunner runner;
	runner.addTest (GuiTestCase::suite());
	runner.run();

/* --- Test Runner ๋งŒ ์‹คํ–‰๋˜๋„๋ก, ๋‹ค์ด์–ผ๋กœ๊ทธ ๋„์šฐ๋Š” ๋ถ€๋ถ„์„ ์ผ๋‹จ ๋ง‰์•„๋‘”๋‹ค. ๋งˆ์ง€๋ง‰์— ์‹คํ–‰ํ• ๋•Œ ์ด ๋ถ€๋ถ„์˜ ์ฃผ์„์ฒ˜๋ฆฌ ๋œ ๋ถ€๋ถ„์„ ํ’€์–ด์ฃผ๋ฉด ๋œ๋‹ค.
#ifdef _AFXDLL
	Enable3dControls();			// Call this when using MFC in a shared DLL
#else
	Enable3dControlsStatic();	// Call this when linking to MFC statically
#endif

	CGuiTestingOneDlg dlg;
	m_pMainWnd = &dlg;
	int nResponse = dlg.DoModal();
	if (nResponse == IDOK)
	{
	}
	else if (nResponse == IDCANCEL)
	{
	}
*/
	return FALSE;
}

2. Test Case ์˜ ์ž‘์„ฑ


1. ์—ฐ์Šต

์ผ๋‹จ, ์ œ๋Œ€๋กœ ์ž‘๋™ํ•˜๋Š”์ง€ ์•Œ์•„๋ณด๊ธฐ ์œ„ํ•ด ์—ฐ์Šต๊ฒธ m_bFlag ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์„œ ํ…Œ์ŠคํŠธ๋ฅผ ํ–ˆ๋‹ค.

ํ…Œ์ŠคํŠธ ํ†ต๊ณผ์‹œ ์˜ˆ์ƒ๋˜๋Š” ๊ฒฐ๊ณผ
test1One ์—ฐ์Šต๊ฒธ Assert Equals. 10 == 10
test2GuiOne ๋ฒ„ํŠผ์ด ๋ˆŒ๋Ÿฌ์กŒ์„ ๊ฒฝ์šฐ์— CGuiTestingOneDlg::m_bFlag ๊ฐ€ true ์ƒํƒœ๊ฐ€ ๋œ๋‹ค.

~cpp 
#include <cppunit/TestCase.h>
#include <cppunit/Extensions/HelperMacros.h>

#include "stdafx.h"                                  // resource, mfc ๋ฅผ ์ด์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
#include "GuiTestingOneDlg.h"                        // import GuiTestingOneDlg 

class GuiTestCase : public CppUnit::TestCase {
	CPPUNIT_TEST_SUITE(GuiTestCase);
	CPPUNIT_TEST ( test1One );
	CPPUNIT_TEST ( test2GuiOne );
	CPPUNIT_TEST ( test3ListAdd );
	CPPUNIT_TEST_SUITE_END();

public:
	CGuiTestingOneDlg* pDlg;
	void setUp () {
		pDlg = new CGuiTestingOneDlg();
		pDlg->Create(IDD_GUITESTINGONE_DIALOG);
	}

	void tearDown () {
		delete pDlg;
	}

public:
	void test1One () {
		CPPUNIT_ASSERT_EQUAL (10, 10);
	}

	void test2GuiOne () {

		pDlg->OnButtonadd();
		CPPUNIT_ASSERT_EQUAL (true, pDlg->m_bFlag);
	}
};

์ฝ”๋“œ ์ถ”๊ฐ€
~cpp 
void CGuiTestingOneDlg::OnButtonadd() 
{
	// TODO: Add your control notification handler code here
	m_bFlag = true;
}

2. ํ…์ŠคํŠธ ์ถ”๊ฐ€

ํ…Œ์ŠคํŠธ ํ†ต๊ณผ์‹œ ์˜ˆ์ƒ๋˜๋Š” ๊ฒฐ๊ณผ
test1One ์—ฐ์Šต๊ฒธ Assert Equals. 10 == 10
test2GuiOne ๋ฒ„ํŠผ์ด ๋ˆŒ๋Ÿฌ์กŒ์„ ๊ฒฝ์šฐ์— CGuiTestingOneDlg::m_bFlag ๊ฐ€ true ์ƒํƒœ๊ฐ€ ๋œ๋‹ค.
test3ListAdd Editbox ์— "Testing..." ์„ ์…‹ํŒ…. ๋ฒ„ํŠผ์„ ๋ˆŒ๋ €์„ ๋•Œ Listbox ์˜ item ๊ฐฏ์ˆ˜๊ฐ€ 1๊ฐœ์ž„์„ ํ™•์ธ
. Listbox ์˜ ์ฒซ๋ฒˆ์งธ item ์˜ ์ŠคํŠธ๋ง์ด "Testing..." ์ž„์„ ํ™•์ธ

~cpp 
	void test3ListAdd () {
		pDlg->m_ctlEdit.SetWindowText("Testing...");
		pDlg->OnButtonadd();
		CPPUNIT_ASSERT_EQUAL (1, pDlg->m_ctlList.GetCount());
		CString str;

		pDlg->m_ctlList.GetText(0, str);
		CPPUNIT_ASSERT ( isSameString(str, "Testing..."));
	}

	bool isSameString (CString& str1, PSTR str2) {
		return str1.Compare(str2) == 0;
	}
};

์ฝ”๋“œ ์ถ”๊ฐ€
~cpp 
void CGuiTestingOneDlg::OnButtonadd() 
{
	// TODO: Add your control notification handler code here
	m_bFlag = true;
	CString str;
	m_ctlEdit.GetWindowText(str);
	m_ctlList.AddString(str);
}

3. more...

ํ…Œ์ŠคํŠธ ํ†ต๊ณผ์‹œ ์˜ˆ์ƒ๋˜๋Š” ๊ฒฐ๊ณผ
test1One ์—ฐ์Šต๊ฒธ Assert Equals. 10 == 10
test2GuiOne ๋ฒ„ํŠผ์ด ๋ˆŒ๋Ÿฌ์กŒ์„ ๊ฒฝ์šฐ์— CGuiTestingOneDlg::m_bFlag ๊ฐ€ true ์ƒํƒœ๊ฐ€ ๋œ๋‹ค.
test3ListAdd Editbox ์— "Testing..." ์„ ์…‹ํŒ…. ๋ฒ„ํŠผ์„ ๋ˆŒ๋ €์„ ๋•Œ Listbox ์˜ item ๊ฐฏ์ˆ˜๊ฐ€ 1๊ฐœ์ž„์„ ํ™•์ธ
. Listbox ์˜ ์ฒซ๋ฒˆ์งธ item ์˜ ๋ฌธ์ž์—ด์ด "Testing..." ์ž„์„ ํ™•์ธ
test4ListAddMore test3 ์— ์ถ”๊ฐ€๋œ ํ˜•ํƒœ. Editbox ์— ๋‹ค์‹œ "Testing2..." ๋ฅผ ์…‹ํŒ…ํ•˜๊ณ , ๋ฒ„ํŠผ์„ ๋ˆŒ๋ €์„ ๋•Œ Listbox ์˜ item ๊ฐฏ์ˆ˜๊ฐ€ 2๊ฐœ์ž„์„ ํ™•์ธ
. Listbox ์˜ ๋‘๋ฒˆ์งธ item ์˜ ๋ฌธ์ž์—ด์ด "Testing2..." ์ž„์„ ํ™•์ธ

~cpp 
	void test4ListAddMore () {
		test3ListAdd();
		CString str;

		pDlg->m_ctlEdit.SetWindowText("Testing2...");
		pDlg->OnButtonadd();
		CPPUNIT_ASSERT_EQUAL (2, pDlg->m_ctlList.GetCount());
		pDlg->m_ctlList.GetText(1, str);
		CPPUNIT_ASSERT ( isSameString(str, "Testing2..."));
	}

4. 'List box ์—์„œ์˜ ์ปค์„œ๋Š” ์ฑ„์›Œ์ง„ ๊ธ€์— ์œ„์น˜ํ•œ๋‹ค' ์— ๋Œ€ํ•œ ํ…Œ์ŠคํŠธ

ํ…Œ์ŠคํŠธ ํ†ต๊ณผ์‹œ ์˜ˆ์ƒ๋˜๋Š” ๊ฒฐ๊ณผ
test1One ์—ฐ์Šต๊ฒธ Assert Equals. 10 == 10
test2GuiOne ๋ฒ„ํŠผ์ด ๋ˆŒ๋Ÿฌ์กŒ์„ ๊ฒฝ์šฐ์— CGuiTestingOneDlg::m_bFlag ๊ฐ€ true ์ƒํƒœ๊ฐ€ ๋œ๋‹ค.
test3ListAdd Editbox ์— "Testing..." ์„ ์…‹ํŒ…. ๋ฒ„ํŠผ์„ ๋ˆŒ๋ €์„ ๋•Œ Listbox ์˜ item ๊ฐฏ์ˆ˜๊ฐ€ 1๊ฐœ์ž„์„ ํ™•์ธ
. Listbox ์˜ ์ฒซ๋ฒˆ์งธ item ์˜ ๋ฌธ์ž์—ด์ด "Testing..." ์ž„์„ ํ™•์ธ
test4ListAddMore test3 ์— ์ถ”๊ฐ€๋œ ํ˜•ํƒœ. Editbox ์— ๋‹ค์‹œ "Testing2..." ๋ฅผ ์…‹ํŒ…ํ•˜๊ณ , ๋ฒ„ํŠผ์„ ๋ˆŒ๋ €์„ ๋•Œ Listbox ์˜ item ๊ฐฏ์ˆ˜๊ฐ€ 2๊ฐœ์ž„์„ ํ™•์ธ
. Listbox ์˜ ๋‘๋ฒˆ์งธ item ์˜ ๋ฌธ์ž์—ด์ด "Testing2..." ์ž„์„ ํ™•์ธ
test5ListCursorOne test3 ์ง„ํ–‰์‹œ ์„ ํƒ๋œ Item ์ด 0 ๋ฒˆ์งธ ์ž„์„ ํ™•์ธ
test6ListCursorTwo test4 ์ง„ํ–‰์‹œ ์„ ํƒ๋œ Item ์ด 1 ๋ฒˆ์งธ ์ž„์„ ํ™•์ธ

~cpp 
	void test5ListCursorOne () {
		test3ListAdd();
		CPPUNIT_ASSERT_EQUAL(0, pDlg->m_ctlList.GetCurSel());
	}

	void test6ListCursorTwo () {
		test4ListAddMore();
		CPPUNIT_ASSERT_EQUAL(1, pDlg->m_ctlList.GetCurSel());
	}


์ฝ”๋“œ ์ถ”๊ฐ€
~cpp 
void CGuiTestingOneDlg::OnButtonadd() 
{
	// TODO: Add your control notification handler code here
	CString str;
	m_ctlEdit.GetWindowText(str);
	m_ctlList.AddString(str);
	m_ctlList.SetCurSel(m_ctlList.GetCount()-1);
	m_bFlag = true;
}


5. edit box ์˜ ๋‚ด์šฉ์ด ๋ฐ์ดํ„ฐ ์ถ”๊ฐ€ํ›„ ์ดˆ๊ธฐํ™” ๋˜๋Š”์ง€ ํ™•์ธ

ํ…Œ์ŠคํŠธ ํ†ต๊ณผ์‹œ ์˜ˆ์ƒ๋˜๋Š” ๊ฒฐ๊ณผ
test1One ์—ฐ์Šต๊ฒธ Assert Equals. 10 == 10
test2GuiOne ๋ฒ„ํŠผ์ด ๋ˆŒ๋Ÿฌ์กŒ์„ ๊ฒฝ์šฐ์— CGuiTestingOneDlg::m_bFlag ๊ฐ€ true ์ƒํƒœ๊ฐ€ ๋œ๋‹ค.
test3ListAdd Editbox ์— "Testing..." ์„ ์…‹ํŒ…. ๋ฒ„ํŠผ์„ ๋ˆŒ๋ €์„ ๋•Œ Listbox ์˜ item ๊ฐฏ์ˆ˜๊ฐ€ 1๊ฐœ์ž„์„ ํ™•์ธ
. Listbox ์˜ ์ฒซ๋ฒˆ์งธ item ์˜ ๋ฌธ์ž์—ด์ด "Testing..." ์ž„์„ ํ™•์ธ
test4ListAddMore test3 ์— ์ถ”๊ฐ€๋œ ํ˜•ํƒœ. Editbox ์— ๋‹ค์‹œ "Testing2..." ๋ฅผ ์…‹ํŒ…ํ•˜๊ณ , ๋ฒ„ํŠผ์„ ๋ˆŒ๋ €์„ ๋•Œ Listbox ์˜ item ๊ฐฏ์ˆ˜๊ฐ€ 2๊ฐœ์ž„์„ ํ™•์ธ
. Listbox ์˜ ๋‘๋ฒˆ์งธ item ์˜ ๋ฌธ์ž์—ด์ด "Testing2..." ์ž„์„ ํ™•์ธ
test5ListCursorOne test3 ์ง„ํ–‰์‹œ ์„ ํƒ๋œ Item ์ด 0 ๋ฒˆ์งธ ์ž„์„ ํ™•์ธ
test6ListCursorTwo test4 ์ง„ํ–‰์‹œ ์„ ํƒ๋œ Item ์ด 1 ๋ฒˆ์งธ ์ž„์„ ํ™•์ธ
test7EmptyEditbox test3 ์ง„ํ–‰์‹œ Editbox ์˜ ์ŠคํŠธ๋ง์ด ๋น„์—ˆ๋Š”์ง€ ํ™•์ธ

~cpp 
	void test7EmptyEditbox () {
		test3ListAdd();
		CString str;

		pDlg->m_ctlEdit.GetWindowText(str);
		CPPUNIT_ASSERT (str.IsEmpty());
	}

์ฝ”๋“œ ์ถ”๊ฐ€
~cpp 
void CGuiTestingOneDlg::OnButtonadd() 
{
	// TODO: Add your control notification handler code here
	CString str;
	m_ctlEdit.GetWindowText(str);
	m_ctlList.AddString(str);
	m_ctlList.SetCurSel(m_ctlList.GetCount()-1);
	m_ctlEdit.SetWindowText("");
	m_bFlag = true;
}

์—ฌ๊ธฐ๊นŒ์ง€๋กœ ์ƒ๊ฐํ•ด๋†“์€ ํ…Œ์ŠคํŠธ๋“ค์ด ์ „๋ถ€ ์™„๋ฃŒ. ์•ž์— InitInstance ์— ์จ ๋„ฃ์€ ์ฃผ์„์„ ํ’€๊ณ , ์‹ค์ œ๋กœ ์‹คํ–‰ํ•ด๋ณด์ž.
~cpp 
BOOL CGuiTestingOneApp::InitInstance()
{
	AfxEnableControlContainer();

	CppUnit::MfcUi::TestRunner runner;
	runner.addTest (GuiTestCase::suite());
	runner.run();

#ifdef _AFXDLL
	Enable3dControls();			// Call this when using MFC in a shared DLL
#else
	Enable3dControlsStatic();	// Call this when linking to MFC statically
#endif

	CGuiTestingOneDlg dlg;
	m_pMainWnd = &dlg;
	int nResponse = dlg.DoModal();
	if (nResponse == IDOK)
	{
	}
	else if (nResponse == IDCANCEL)
	{
	}

	return FALSE;
}


๋ฌธ์ œ์ 

  • ๋ชจ๋‹ฌ๋ฆฌ์Šค ๋‹ค์ด์–ผ๋กœ๊ทธ์ธ ๊ด€๊ณ„๋กœ, ํ…Œ์ŠคํŠธ๋ฅผ run ์œผ๋กœ ์‹คํ–‰ํ•  ๋•Œ ๋งˆ๋‹ค Dialog ๊ฐ€ ์ผœ์กŒ๋‹ค ๊บผ์กŒ๋‹ค ํ•œ๋‹ค. ์ด์— ๋”ฐ๋ฅธ ์†๋„์˜ ์ง€์—ฐ์˜ ๋ฌธ์ œ. -> CDialog::ShowWindow(SH_HIDE); ๋กœ ํ•ด๊ฒฐ ๊ฐ€๋Šฅ


Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:23:20
Processing time 0.0146 sec