import{_ as i,c as a,o as e,ae as t}from"./chunks/framework.l7bN90nj.js";const r=JSON.parse('{"title":"Test and Debug","description":"","frontmatter":{},"headers":[],"relativePath":"zh/dev/test-and-debug.md","filePath":"zh/dev/test-and-debug.md"}'),l={name:"zh/dev/test-and-debug.md"};function n(p,s,h,k,d,c){return e(),a("div",null,[...s[0]||(s[0]=[t(`<h1 id="test-and-debug" tabindex="-1">Test and Debug <a class="header-anchor" href="#test-and-debug" aria-label="Permalink to &quot;Test and Debug&quot;">​</a></h1><h2 id="run-tests" tabindex="-1">Run Tests <a class="header-anchor" href="#run-tests" aria-label="Permalink to &quot;Run Tests&quot;">​</a></h2><p>clice 有两种形式的测试，单元测试和集成测试。</p><ul><li>运行单元测试</li></ul><div class="language-bash vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">bash</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">$</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> ./build/bin/unit_tests</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --test-dir=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;./tests/data&quot;</span></span></code></pre></div><ul><li>运行集成测试</li></ul><p>我们使用 pytest 来运行集成测试，请参考 <code>pyproject.toml</code> 安装依赖的 python 库</p><div class="language-bash vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">bash</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">$</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> pytest</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -s</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --log-cli-level=INFO</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> tests/integration</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --executable=./build/bin/clice</span></span></code></pre></div><p>如果你使用 xmake 作为构建系统，可以直接通过 xmake 运行测试：</p><div class="language-shell vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">$</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> xmake</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> run</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --verbose</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> unit_tests</span></span>
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">$</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> xmake</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> test</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --verbose</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> integration_tests/default</span></span></code></pre></div><h2 id="debug" tabindex="-1">Debug <a class="header-anchor" href="#debug" aria-label="Permalink to &quot;Debug&quot;">​</a></h2><p>如果想在 clice 上附加调试器并进行调试，推荐先单独以 socket 模式启动 clice，然后再将客户端连接到 clice 上</p><div class="language-shell vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">$</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> ./build/bin/clice</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --mode=socket</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --port=50051</span></span></code></pre></div><p>在服务器启动之后，可以通过以下两种方式启动客户端连接到服务器</p><ul><li>使用 pytest 运行特定测试进行连接</li></ul><p>你可以运行一个单独的集成测试用例来连接正在运行的 clice。这对于复现和调试特定场景非常有用。</p><div class="language-shell vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">$</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> pytest</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -s</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --log-cli-level=INFO</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> tests/integration/test_file_operation.py::test_did_open</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --mode=socket</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --port=50051</span></span></code></pre></div><ul><li>使用 vscode 进行实际的测试</li></ul><p>你也可以通过配置 clice-vscode 插件来连接正在运行的 clice 服务，从而在实际使用场景中进行调试。</p><ol><li><p>在插件市场下载插件 <a href="https://marketplace.visualstudio.com/items?itemName=ykiko.clice-vscode" target="_blank" rel="noreferrer">clice-vscode</a></p></li><li><p>配置 <code>settings.json</code>: 在你的项目根目录下创建 <code>.vscode/settings.json</code> 文件，并填入以下内容：</p><div class="language-jsonc vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">jsonc</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">{</span></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">  // Point this to the clice binary you downloaded.</span></span>
<span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">  &quot;clice.executable&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;/path/to/your/clice/executable&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">,</span></span>
<span class="line"></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">  // Enable socket mode.</span></span>
<span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">  &quot;clice.mode&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;socket&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">,</span></span>
<span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">  &quot;clice.port&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">50051</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">,</span></span>
<span class="line"></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">  // Optional: Set this to an empty string to turn off the clangd.</span></span>
<span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">  &quot;clangd.path&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">,</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre></div></li><li><p>重新加载窗口：修改配置后，在 vscode 中执行 Developer: Reload Window 命令使配置生效。插件会自动连接到正在 50051 端口监听的 clice。</p></li></ol><p>如果你需要修改或调试 clice-vscode 插件本身，可以按以下步骤操作：</p><ol><li><p>克隆并安装依赖：</p><div class="language-shell vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">$</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> git</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> clone</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> https://github.com/clice-io/clice-vscode</span></span>
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">$</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> cd</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> clice-vscode</span></span>
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">$</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> npm</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> install</span></span></code></pre></div></li><li><p>使用 vscode 打开插件项目：用一个新的 vscode 窗口打开 clice-vscode 文件夹</p></li><li><p>创建调试配置：在 clice-vscode 项目中，也创建一个 <code>.vscode/settings.json</code> 文件，内容与上方相同</p></li><li><p>按下 <code>F5</code> 键。这会启动一个【扩展开发宿主】窗口。这是一个加载了你本地 clice-vscode 插件代码的新的 vscode 窗口，在这个新窗口中打开你的 C++ 项目，它应该会自动连接到 clice</p></li></ol>`,22)])])}const g=i(l,[["render",n]]);export{r as __pageData,g as default};
