All Projects → ez8-co → emock

ez8-co / emock

Licence: Apache-2.0 License
🐞 下一代C/C++跨平台mock库 (Next generation cross-platform mock library for C/C++)

Programming Languages

C++
36643 projects - #6 most used programming language
c
50402 projects - #5 most used programming language
CMake
9771 projects

Projects that are alternatives of or similar to emock

gmock-xcode
Xcode integration for GoogleMock through XCTest
Stars: ✭ 18 (-75.34%)
Mutual labels:  gmock, googlemock, gtest
Swiftymocky
Framework for automatic mock generation. Adds a set of handy methods, simplifying testing. One of the best and most complete solutions, including generics support and much more.
Stars: ✭ 748 (+924.66%)
Mutual labels:  mock, unittest
how-to-qemu-arm-gdb-gtest
How to run, debug, and unit test ARM code on X86 ubuntu
Stars: ✭ 19 (-73.97%)
Mutual labels:  gmock, gtest
ja-rule
Ja Rule is the code behind OLE, an Open Source DMX/RDM framework for PIC32 microcontrollers
Stars: ✭ 36 (-50.68%)
Mutual labels:  gmock, gtest
Testable Mock
换种思路写Mock,让单元测试更简单
Stars: ✭ 977 (+1238.36%)
Mutual labels:  mock, unittest
Unit Threaded
Advanced unit test framework for D
Stars: ✭ 100 (+36.99%)
Mutual labels:  mock, unittest
Pytruth
Assertion framework for Python unit tests
Stars: ✭ 118 (+61.64%)
Mutual labels:  mock, unittest
Fake Xrm Easy
The testing framework for Dynamics CRM and Dynamics 365 which runs on an In-Memory context and deals with mocks or fakes for you
Stars: ✭ 216 (+195.89%)
Mutual labels:  mock, unittest
local-data-api
Data API for local, you can write unittest for AWS Aurora Serverless's Data API
Stars: ✭ 99 (+35.62%)
Mutual labels:  mock, unittest
InstantMock
Create mocks easily in Swift
Stars: ✭ 88 (+20.55%)
Mutual labels:  mock
deckard
DNS test harness
Stars: ✭ 28 (-61.64%)
Mutual labels:  mock
ccxx
This is a cross-platform library software library about c, c ++, unix4, posix. Include gtest, benchmark, cmake, process lock, daemon, libuv, lua, cpython, re2, json, yaml, mysql, redis, opencv, qt, lz4, oci ... https://hub.docker.com/u/oudream
Stars: ✭ 31 (-57.53%)
Mutual labels:  gtest
flake8-assertive
Flake8 unittest assert method checker
Stars: ✭ 30 (-58.9%)
Mutual labels:  unittest
tsioc
AOP, Ioc container, Boot framework, unit testing framework , activities workflow framework.
Stars: ✭ 15 (-79.45%)
Mutual labels:  unittest
googletest-ci
Continuous integration (CI) + Google Test (gtest) + CMake example boilerplate demo
Stars: ✭ 14 (-80.82%)
Mutual labels:  gtest
helm-unittest
BDD styled unit test framework for Kubernetes Helm charts as a Helm plugin.
Stars: ✭ 276 (+278.08%)
Mutual labels:  unittest
Wasmite
Now WebAssembly has proper testing, unit-testing and debugging 🤗
Stars: ✭ 20 (-72.6%)
Mutual labels:  unittest
vite-vue-admin
🎉🎉使用Vite + Vue3 + TypeScript + Element-plus + Mock开发的后台管理系统🎉🎉
Stars: ✭ 97 (+32.88%)
Mutual labels:  mock
webapi
WAI based library for web api
Stars: ✭ 27 (-63.01%)
Mutual labels:  mock
animaris
Documentation and Mock for JSBridge base on ThinkJS & MongoDB & React & Antd.
Stars: ✭ 28 (-61.64%)
Mutual labels:  mock

English README

EMOCK license Build Status

  • EMOCK是基于mockcpp核心改进的下一代C/C++跨平台mock库
    • 【使用简单】 学习成本低,仅一个宏
    • 【没有依赖】 除了STL和系统库函数
    • 【跨平台】 支持主流32/64位系统(*nix、Windows)
    • 【完全支持】 支持mock所有类型的函数
    • 【非侵入设计】 不需要调整任何待测代码

正在开发

  • 🐮 类似反射功能支持,无需声明即可mock(支持不可见的静态全局函数和动态库内部函数)
    • linux下改用dwarf实现
    • 支持C语言声明的函数
  • 支持mock带有异常约束的函数
  • 默认单元测试框架改用gtest
  • 缺失的caller匹配功能

最新支持

  • 统一的EMOCK宏(测试虚函数,不再需要用户添加控制反转支持)
  • 支持mock可变参数函数,例如:int test(int a, ...)
  • 支持mock重载的成员函数(Windows系统)
  • 解决老的获取成员函数方法会有警告问题(Linux系统)
  • 使用蹦床使得this指针可见(Windows系统)
  • 👏 使用短跳+蹦床的方法解决x64下非期望覆盖较小函数的问题

特性矩阵

下面是和一些主流mock库的对比(如有错误,不吝指正)

平台 成员函数 普通函数 杂项
Linux Windows 虚函数 普通 静态 全局 变参 模板 无需修改
EMOCK
CppUMock [0]
mockcpp [1]
googlemock [2]
mockitopp [1]
C-Mock [1]
CppFreeMock [1]
  • 注:
    • [0]: 需要IoC接口,派生并实现待测类的虚成员函数
    • [1]: 需要定义接口(只有纯虚成员函数),不支持混合类(同时包含虚成员函数和普通成员函数的类)
    • [2]: 需要IoC接口,需要声明含有待测虚成员函数的派生类(相同参数列表和返回类型)
    • [0][1][2]: 无法测试类内嵌对象或者引用

与使用API HOOK技术的库对比

跳转安全 this指针可见[Windows] 备注
EMOCK 使用蹦床将远跳变为短跳
mockcpp x64下直接使用14字节的远跳
CppFreeMock x64下直接使用14字节的远跳
  • EMOCK理论上也支持UNIX, Android, MacOS和iOS等*nix系统,但可能需要少量改动

快速概览

全局函数

    // 待测函数
    int foobar(int x) {
        return x;
    }

    // 测试时,像下面这样就可以mock
    EMOCK(foobar)
        .stubs()
        .with(any()) // 约束匹配任意输入
        .will(returnValue(1)); // 调用时返回1

    // 调用会返回1
    ASSERT_EQ(foobar(0), 1);

成员函数

    // 待测成员函数
    class Foo
    {
    public:
        void bar1(int);
        virtual void bar2(double);
        static int bar3();
    };

    ////////////////////////////////////

    // 指定调用的mock函数
    void EMOCK_API mock_bar1(Foo* obj, int) {
        // ...
    }
    void EMOCK_API mock_bar2(Foo* obj, double) {
        // ...
    }

    // 测试时,像下面这样就可以mock
    EMOCK(&Foo::bar1)
        .stubs()
        .will(invoke(mock_bar1)); // 指定调用自定义的函数而不是指定返回值
    EMOCK(&Foo::bar2) // 虚成员函数并不特别
        .stubs()
        .will(invoke(mock_bar2));
    EMOCK(Foo::bar3) // 静态函数类似全局函数,不需要&
        .stubs()
        .will(returnValue(1));

重载函数

    // 待测重载函数
    int foobar(int x) {
        return x;
    }
    double foobar(double x) {
        return x;
    }

    // 重载函数,像下面这样就可以mock
    EMOCK((int (*)(int))foobar)
        .stubs()
        .will(returnValue(1));
    EMOCK(static_cast<double (*)(double)>(foobar))
        .stubs()
        .will(returnValue(1.0));

    // 待测重载成员函数
    class Foo
    {
    public:
        void bar(int);
        void bar(double);
    };

    // 重载的成员函数,像下面这样就可以mock
    EMOCK((void (Foo::*)(int))&Foo::bar)
        .expects(once()); // 只会调用一次
    EMOCK(static_cast<void (Foo::*)(double)>(&Foo::bar))
        .expects(never()); // 不会被调用

使用手册

衷心感谢

已知问题

  • valgrind一起使用
    • 请添加--smc-check=all选项禁用缓存来防止动态修改机器码(API HOOK技术)部分失效导致不可预期的错误
    • 因为valgrind的特殊处理,暂时无法mock系统调用相关函数 (例如 gettimeofday

写在最后

请我喝一杯咖啡

支付宝 微信 PayPal
支付宝二维码 微信二维码 PayPal二维码
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].