From 06eebece1e72152e7f37ab585212707a31f08ef9 Mon Sep 17 00:00:00 2001 From: zhangyongpan Date: Thu, 10 Jul 2025 08:37:45 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=A1=B9=E7=9B=AE=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- UnitTestDemo.sln | 28 +++ .../.opencppcov/UnitTestDemo/Debug_x64.json | 1 + UnitTestDemo/MathLibrary.cpp | 21 +++ UnitTestDemo/MathLibrary.h | 13 ++ UnitTestDemo/UnitTestDemo.cpp | 34 ++++ UnitTestDemo/UnitTestDemo.vcxproj | 175 ++++++++++++++++++ UnitTestDemo/UnitTestDemo.vcxproj.filters | 36 ++++ UnitTestDemo/pch.cpp | 5 + UnitTestDemo/pch.h | 12 ++ 9 files changed, 325 insertions(+) create mode 100644 UnitTestDemo.sln create mode 100644 UnitTestDemo/.opencppcov/UnitTestDemo/Debug_x64.json create mode 100644 UnitTestDemo/MathLibrary.cpp create mode 100644 UnitTestDemo/MathLibrary.h create mode 100644 UnitTestDemo/UnitTestDemo.cpp create mode 100644 UnitTestDemo/UnitTestDemo.vcxproj create mode 100644 UnitTestDemo/UnitTestDemo.vcxproj.filters create mode 100644 UnitTestDemo/pch.cpp create mode 100644 UnitTestDemo/pch.h diff --git a/UnitTestDemo.sln b/UnitTestDemo.sln new file mode 100644 index 0000000..0c0c824 --- /dev/null +++ b/UnitTestDemo.sln @@ -0,0 +1,28 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.12.35728.132 d17.12 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "UnitTestDemo", "UnitTestDemo\UnitTestDemo.vcxproj", "{AC61644F-EC83-47F1-9B52-E0A45B5297A4}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {AC61644F-EC83-47F1-9B52-E0A45B5297A4}.Debug|x64.ActiveCfg = Debug|x64 + {AC61644F-EC83-47F1-9B52-E0A45B5297A4}.Debug|x64.Build.0 = Debug|x64 + {AC61644F-EC83-47F1-9B52-E0A45B5297A4}.Debug|x86.ActiveCfg = Debug|Win32 + {AC61644F-EC83-47F1-9B52-E0A45B5297A4}.Debug|x86.Build.0 = Debug|Win32 + {AC61644F-EC83-47F1-9B52-E0A45B5297A4}.Release|x64.ActiveCfg = Release|x64 + {AC61644F-EC83-47F1-9B52-E0A45B5297A4}.Release|x64.Build.0 = Release|x64 + {AC61644F-EC83-47F1-9B52-E0A45B5297A4}.Release|x86.ActiveCfg = Release|Win32 + {AC61644F-EC83-47F1-9B52-E0A45B5297A4}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/UnitTestDemo/.opencppcov/UnitTestDemo/Debug_x64.json b/UnitTestDemo/.opencppcov/UnitTestDemo/Debug_x64.json new file mode 100644 index 0000000..39d8eff --- /dev/null +++ b/UnitTestDemo/.opencppcov/UnitTestDemo/Debug_x64.json @@ -0,0 +1 @@ +{"BasicSettingController":{"Data":{"ProgramToRun":"C:\\Program Files\\Microsoft Visual Studio\\2022\\Community\\Common7\\IDE\\Extensions\\TestPlatform\\vstest.console.exe","OptionalWorkingDirectory":"C:\\Users\\徐加冠\\source\\repos\\UnitTestDemo\\UnitTestDemo\\","Arguments":"D:\\LIB\\UnitTestDemo\\x64\\Debug\\UnitTestDemo.dll","CompileBeforeRunning":true,"OptimizedBuild":false},"IsSelectedByProjectPath":{"UnitTestDemo\\UnitTestDemo.vcxproj":true}},"FilterSettingController":{"AdditionalSourcePatterns":[],"AdditionalModulePatterns":[],"ExcludedSourcePatterns":[],"ExcludedModulePatterns":[],"UnifiedDiffs":[]},"ImportExportSettingController":{"Exports":[],"InputCoverages":[],"CoverChildrenProcesses":false,"AggregateByFile":true},"MiscellaneousSettingController":{"OptionalConfigFile":null,"LogTypeValue":0,"ContinueAfterCppExceptions":false}} \ No newline at end of file diff --git a/UnitTestDemo/MathLibrary.cpp b/UnitTestDemo/MathLibrary.cpp new file mode 100644 index 0000000..4219300 --- /dev/null +++ b/UnitTestDemo/MathLibrary.cpp @@ -0,0 +1,21 @@ +#include "pch.h" +#include "MathLibrary.h" +double MathLibrary::Divide(double a, double b) { + if (b != 0) return a / b; + return 0.0; +} + +bool MathLibrary::IsPositive(int n) { + return n >= 0; +} + +bool MathLibrary::CheckRange(int a, int b) { + return (a > 0) && (b < 10); +} + +double MathLibrary::CalculateDiscount(int age, bool isMember) { + if (age < 18) + return isMember ? 0.25 : 0.20; + else + return isMember ? 0.15 : 0.10; +} diff --git a/UnitTestDemo/MathLibrary.h b/UnitTestDemo/MathLibrary.h new file mode 100644 index 0000000..216fc22 --- /dev/null +++ b/UnitTestDemo/MathLibrary.h @@ -0,0 +1,13 @@ +#pragma once + +class MathLibrary +{ +public: + static double Divide(double a, double b); + static bool IsPositive(int n); + static bool CheckRange(int a, int b); + static double CalculateDiscount(int age, bool isMember); + +}; + +int myfunc(int a, int b); diff --git a/UnitTestDemo/UnitTestDemo.cpp b/UnitTestDemo/UnitTestDemo.cpp new file mode 100644 index 0000000..962e5dc --- /dev/null +++ b/UnitTestDemo/UnitTestDemo.cpp @@ -0,0 +1,34 @@ +#include "pch.h" +#include "CppUnitTest.h" +#include "MathLibrary.h" + +using namespace Microsoft::VisualStudio::CppUnitTestFramework; + +namespace UnitTestDemo +{ + TEST_CLASS(UnitTestDemo) + { + public: + + // 语句覆盖 + TEST_METHOD(Divide_ZeroDivisor1) { Assert::AreEqual(0.0, MathLibrary::Divide(4, 0)); } + TEST_METHOD(Divide_ZeroDivisor2) { Assert::AreEqual(0.0, MathLibrary::Divide(4, 0)); } + + // 分支覆盖 + TEST_METHOD(IsPositive_Positive1) { Assert::IsTrue(MathLibrary::IsPositive(5)); } + TEST_METHOD(IsPositive_Negative2) { Assert::IsFalse(MathLibrary::IsPositive(-5)); } + + // 条件覆盖 + TEST_METHOD(CheckRange_BothTrue1) { Assert::IsTrue(MathLibrary::CheckRange(5, 5)); } + TEST_METHOD(CheckRange_FirstFalse2) { Assert::IsFalse(MathLibrary::CheckRange(-5, 5)); } + TEST_METHOD(CheckRange_SecondFalse3) { Assert::IsFalse(MathLibrary::CheckRange(5, 15)); } + + // 路径覆盖 + TEST_METHOD(Discount_TeenMember1) { Assert::AreEqual(0.25, MathLibrary::CalculateDiscount(15, true)); } + TEST_METHOD(Discount_TeenNonMember2) { Assert::AreEqual(0.20, MathLibrary::CalculateDiscount(15, false)); } + TEST_METHOD(Discount_AdultMember3) { Assert::AreEqual(0.15, MathLibrary::CalculateDiscount(20, true)); } + TEST_METHOD(Discount_AdultNonMember4) { Assert::AreEqual(0.10, MathLibrary::CalculateDiscount(20, false)); } + + + }; +} diff --git a/UnitTestDemo/UnitTestDemo.vcxproj b/UnitTestDemo/UnitTestDemo.vcxproj new file mode 100644 index 0000000..c4648c8 --- /dev/null +++ b/UnitTestDemo/UnitTestDemo.vcxproj @@ -0,0 +1,175 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 17.0 + {AC61644F-EC83-47F1-9B52-E0A45B5297A4} + Win32Proj + UnitTestDemo + 10.0 + NativeUnitTestProject + + + + DynamicLibrary + true + v143 + Unicode + false + + + DynamicLibrary + false + v143 + true + Unicode + false + + + DynamicLibrary + true + v143 + Unicode + false + + + DynamicLibrary + false + v143 + true + Unicode + false + + + + + + + + + + + + + + + + + + + + + true + + + true + + + false + + + false + + + + Use + Level3 + true + $(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories) + _DEBUG;%(PreprocessorDefinitions) + true + pch.h + + + Windows + $(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) + + + + + Use + Level3 + true + $(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories) + WIN32;_DEBUG;%(PreprocessorDefinitions) + true + pch.h + + + Windows + $(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) + + + + + Use + Level3 + true + true + true + $(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories) + WIN32;NDEBUG;%(PreprocessorDefinitions) + true + pch.h + + + Windows + true + true + $(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) + + + + + Use + Level3 + true + true + true + $(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories) + NDEBUG;%(PreprocessorDefinitions) + true + pch.h + + + Windows + true + true + $(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) + + + + + + Create + Create + Create + Create + + + + + + + + + + + \ No newline at end of file diff --git a/UnitTestDemo/UnitTestDemo.vcxproj.filters b/UnitTestDemo/UnitTestDemo.vcxproj.filters new file mode 100644 index 0000000..0f98313 --- /dev/null +++ b/UnitTestDemo/UnitTestDemo.vcxproj.filters @@ -0,0 +1,36 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + + + 源文件 + + + 源文件 + + + 源文件 + + + + + 源文件\头文件 + + + 源文件\头文件 + + + \ No newline at end of file diff --git a/UnitTestDemo/pch.cpp b/UnitTestDemo/pch.cpp new file mode 100644 index 0000000..b6fb8f4 --- /dev/null +++ b/UnitTestDemo/pch.cpp @@ -0,0 +1,5 @@ +// pch.cpp: 与预编译标头对应的源文件 + +#include "pch.h" + +// 当使用预编译的头时,需要使用此源文件,编译才能成功。 diff --git a/UnitTestDemo/pch.h b/UnitTestDemo/pch.h new file mode 100644 index 0000000..69fcd40 --- /dev/null +++ b/UnitTestDemo/pch.h @@ -0,0 +1,12 @@ +// pch.h: 这是预编译标头文件。 +// 下方列出的文件仅编译一次,提高了将来生成的生成性能。 +// 这还将影响 IntelliSense 性能,包括代码完成和许多代码浏览功能。 +// 但是,如果此处列出的文件中的任何一个在生成之间有更新,它们全部都将被重新编译。 +// 请勿在此处添加要频繁更新的文件,这将使得性能优势无效。 + +#ifndef PCH_H +#define PCH_H + +// 添加要在此处预编译的标头 + +#endif //PCH_H