使用 pytest 的 Mojo🔥 测试运行程序。
此软件包实现了一个插件来发现和运行 Mojo 测试。虽然没有 Mojo 源代码或包结构的感知是可扩展的。通过遵循约定,您可以使用运行 Mojo 测试并查看结果。总之,调用子进程并解析输出 和退出代码。pytest
plugin.py
mojo run
Convention
- 在项目根目录下创建 Mojo 包目录。
- 在 Mojo 包中的任意位置创建以 、ex: 或 为前缀的源文件。 后缀为 的文件也会被收集,例如:或
.test_test_something.mojotest_xyz.🔥
_testsomething_test.mojoxyz_test.🔥
- 每个测试文件都必须具有入口点函数。
main
- 每个函数都可以调用 Mojo 包中的任意函数和方法。
main
- 每个测试项目必须打印一行测试名称,以 (hashtag, comment) 字符为前缀,例如:.
#``# test name
- 失败的测试应使用标准测试断言之一。 中提供了用于测试的帮助程序结构。如果 Mojo 是 引发或发生未处理的异常。请注意,对于未经处理的异常,后续测试将不会 收集。
example/tests/util.mojo
Error
- Mojo 编译器警告可以选择使用该模式作为测试失败进行处理。
pytest -W error
用法
1、在 Python 环境中安装 >= 7.4。pytest
2、使用 或 Conda environment.yaml 安装插件。pytest-mojo
pip install git+https://github.com/guidorice/mojo-pytest.git
3、使用此处所述的项目布局。
4、从项目根目录运行。另请参阅 pytest 文档。例子:pytest
# summary
pytest
# details
pytest -v
# mojo warnings treated as errors
pytest -W error
# show all captured stdout
pytest -s
示例项目
目录中有一个包含几个模块的 Mojo 包。注意:每个子目录中也存在一个文件,此处未显示:example/__init__.mojo
example
├── mod_a
│ └── impl.mojo
├── mod_b
│ └── impl.mojo
└── tests
├── mod_a
│ ├── test_convert.mojo
│ ├── test_convert_different.mojo
│ └── test_maths.mojo
├── mod_b
│ └── test_greet.mojo
├── suffix_test.mojo
├── test_warning.mojo
└── util.mojo
$ pytest
================================ test session starts ================================
platform darwin -- Python 3.11.5, pytest-7.4.0, pluggy-1.0.0
rootdir: /Users/guidorice/mojo/mojo-pytest
plugins: mojo-0.2.0
collected 17 items
example/tests/suffix_test.mojo . [ 5%]
example/tests/test_warning.mojo . [ 11%]
example/tests/mod_a/test_convert.mojo . [ 17%]
example/tests/mod_a/test_convert_different.mojo . [ 23%]
example/tests/mod_a/test_maths.mojo ....F....... [ 94%]
example/tests/mod_b/test_greet.mojo . [100%]
===================================== FAILURES ======================================
__________________________________ maths more: 42 __________________________________
(<MojoTestItem maths more: 42>, 'ASSERT ERROR: bad maths: 42')
============================== short test summary info ==============================
FAILED example/tests/mod_a/test_maths.mojo:: maths more: 42
=========================== 1 failed, 16 passed in 0.38s ============================
开源地址:
https://github.com/guidorice/mojo-pytest