PageSourceSearch

https://wasmedge.org/docs/assets/js/9559bbea.4779a82c.js

js wasmedge.org collected 2026-10-02 16:39:32 UTC 7,899 bytes, 1 lines download raw bytes

1"use strict";(self.webpackChunkbook=self.webpackChunkbook||[]).push([[462],{3905:(e,t,n)=>{n.d(t,{Zo:()=>p,kt:()=>m});var i=n(67294);function s(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function r(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var i=Object.getOwnPropertySymbols(e);t&&(i=i.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,i)}return n}function o(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?r(Object(n),!0).forEach((function(t){s(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):r(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}function a(e,t){if(null==e)return{};var n,i,s=function(e,t){if(null==e)return{};var n,i,s={},r=Object.keys(e);for(i=0;i<r.length;i++)n=r[i],t.indexOf(n)>=0||(s[n]=e[n]);return s}(e,t);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);for(i=0;i<r.length;i++)n=r[i],t.indexOf(n)>=0||Object.prototype.propertyIsEnumerable.call(e,n)&&(s[n]=e[n])}return s}var l=i.createContext({}),u=function(e){var t=i.useContext(l),n=t;return e&&(n="function"==typeof e?e(t):o(o({},t),e)),n},p=function(e){var t=u(e.components);return i.createElement(l.Provider,{value:t},e.children)},c="mdxType",d={inlineCode:"code",wrapper:function(e){var t=e.children;return i.createElement(i.Fragment,{},t)}},g=i.forwardRef((function(e,t){var n=e.components,s=e.mdxType,r=e.originalType,l=e.parentName,p=a(e,["components","mdxType","originalType","parentName"]),c=u(n),g=s,m=c["".concat(l,".").concat(g)]||c[g]||d[g]||r;return n?i.createElement(m,o(o({ref:t},p),{},{components:n})):i.createElement(m,o({ref:t},p))}));function m(e,t){var n=arguments,s=t&&t.mdxType;if("string"==typeof e||s){var r=n.length,o=new Array(r);o[0]=g;var a={};for(var l in t)hasOwnProperty.call(t,l)&&(a[l]=t[l]);a.originalType=e,a[c]="string"==typeof e?e:s,o[1]=a;for(var u=2;u<r;u++)o[u]=n[u];return i.createElement.apply(null,o)}return i.createElement.apply(null,n)}g.displayName="MDXCreateElement"},21322:(e,t,n)=>{n.r(t),n.d(t,{assets:()=>l,contentTitle:()=>o,default:()=>d,frontMatter:()=>r,metadata:()=>a,toc:()=>u});var i=n(87462),s=(n(67294),n(3905));const r={sidebar_position:5},o="Writing Tests for WasmEdge Plugins",a={unversionedId:"contribute/plugin/test_plugin",id:"contribute/plugin/test_plugin",title:"Writing Tests for WasmEdge Plugins",description:"This guide aims to help you write tests for your newly developed WasmEdge plugin. We will cover the steps of creating test cases, implementing the test cases in code, and executing them with Google Test, a popular C++ testing framework.",source:"@site/docs/contribute/plugin/test_plugin.md",sourceDirName:"contribute/plugin",slug:"/contribute/plugin/test_plugin",permalink:"/docs/contribute/plugin/test_plugin",draft:!1,editUrl:"https://github.com/wasmedge/docs/blob/main/docs/contribute/plugin/test_plugin.md",tags:[],version:"current",sidebarPosition:5,frontMatter:{sidebar_position:5},sidebar:"contributeSidebar",previous:{title:"Develop WasmEdge Plug-in in Rust SDK with witc",permalink:"/docs/contribute/plugin/develop_plugin_rustsdk"},next:{title:"Best Practice",permalink:"/docs/contribute/plugin/best_practice"}},l={},u=[{value:"Understanding Your Plugin",id:"understanding-your-plugin",level:2},{value:"Creating Test Cases",id:"creating-test-cases",level:2},{value:"Implementing Test Cases",id:"implementing-test-cases",level:2},{value:"Compiling and Executing Tests",id:"compiling-and-executing-tests",level:2}],p={toc:u},c="wrapper";function d(e){let{components:t,...n}=e;return(0,s.kt)(c,(0,i.Z)({},p,n,{components:t,mdxType:"MDXLayout"}),(0,s.kt)("h1",{id:"writing-tests-for-wasmedge-plugins"},"Writing Tests for WasmEdge Plugins"),(0,s.kt)("p",null,"This guide aims to help you write tests for your newly developed WasmEdge plugin. We will cover the steps of creating test cases, implementing the test cases in code, and executing them with Google Test, a popular C++ testing framework."),(0,s.kt)("h2",{id:"understanding-your-plugin"},"Understanding Your Plugin"),(0,s.kt)("p",null,"Before you start writing tests, make sure you understand your plugin's functionality and structure. The plugin code for WasmEdge typically consists of the following parts:"),(0,s.kt)("ul",null,(0,s.kt)("li",{parentName:"ul"},(0,s.kt)("strong",{parentName:"li"},"Plugin and Module Descriptions"),": These are structures that provide metadata about the plugin and the modules it includes."),(0,s.kt)("li",{parentName:"ul"},(0,s.kt)("strong",{parentName:"li"},"Host Functions and Modules"),": These are the functionalities provided by the plugin, implemented as C++ classes and methods."),(0,s.kt)("li",{parentName:"ul"},(0,s.kt)("strong",{parentName:"li"},"Module Creation Functions"),": These functions create instances of the plugin's modules when the plugin is loaded by the WasmEdge runtime.")),(0,s.kt)("h2",{id:"creating-test-cases"},"Creating Test Cases"),(0,s.kt)("p",null,"The first step in writing tests is to create test cases. Each test case should focus on a 
1specific functionality of your plugin. For example, if your plugin provides a function to add two numbers, you might have test cases that cover normal inputs, edge cases (like the maximum possible integers), and error handling (like passing non-integer values)."),(0,s.kt)("h2",{id:"implementing-test-cases"},"Implementing Test Cases"),(0,s.kt)("p",null,"Once you have your test cases, you can start implementing them in code. Each test case should be implemented as a C++ function that uses Google Test macros to perform assertions."),(0,s.kt)("p",null,"Here's an example of how you might implement a test case:"),(0,s.kt)("pre",null,(0,s.kt)("code",{parentName:"pre"},'```cpp\n#include "gtest/gtest.h"\n#include "your_plugin.h"\n\nTEST(YourPluginTest, ConvertsNormalString) {\nYourPlugin plugin;\nstd::string input = "123";\nint expected = 123;\nEXPECT_EQ(expected, plugin.convert(input));\n}\n```\n')),(0,s.kt)("p",null,"In this example, ",(0,s.kt)("inlineCode",{parentName:"p"},"YourPluginTest")," is the test suite name, and ",(0,s.kt)("inlineCode",{parentName:"p"},"ConvertsNormalString")," is the test case name. The ",(0,s.kt)("inlineCode",{parentName:"p"},"EXPECT_EQ")," macro is used to verify that the result of ",(0,s.kt)("inlineCode",{parentName:"p"},"plugin.convert(input)")," is the same as ",(0,s.kt)("inlineCode",{parentName:"p"},"expected"),"."),(0,s.kt)("h2",{id:"compiling-and-executing-tests"},"Compiling and Executing Tests"),(0,s.kt)("p",null,"The final step is to compile and execute your tests. WasmEdge uses CMake to manage its build process, so you can add your test file to the ",(0,s.kt)("inlineCode",{parentName:"p"},"CMakeLists.txt")," file in the test directory:"),(0,s.kt)("pre",null,(0,s.kt)("code",{parentName:"pre"},"```cmake\nadd_executable(your_plugin_test your_plugin_test.cpp)\ntarget_link_libraries(your_plugin_test gtest_main your_plugin)\nadd_test(NAME your_plugin_test COMMAND your_plugin_test)\n```\n")),(0,s.kt)("p",null,"Then, you can build and run your tests with the following commands:"),(0,s.kt)("pre",null,(0,s.kt)("code",{parentName:"pre"},"```bash\nmkdir build\ncd buildtest_plugin.md\ncmake ..\nmake\nctest\n```\n")),(0,s.kt)("p",null,"If everything is set up correctly, this will compile your tests, run them, and report the results."),(0,s.kt)("p",null,"Remember, testing is an iterative process. As you develop new features or fix bugs, you should also update your tests to reflect these changes. This will ensure that your plugin continues to work as expected as it evolves."),(0,s.kt)("p",null,"We hope this guide helps you get started with writing tests for your WasmEdge plugins!"))}d.isMDXComponent=!0}}]);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.