Nginx Pytest夹具-IPLWEB临时叉
pytest-nginx-iplweb的Python项目详细描述
pytest nginx
pytest nginx是一个pytest插件,它允许您测试代码,这需要 与成熟的web服务器通信。可以制作定制夹具 管理服务器根目录的内容。
用法
插件包含一个固定装置:
- nginx_proc-会话范围的fixture,它用 为静态文件提供服务的最基本配置。
- nginx_php_proc-会话范围的fixture,它管理nginx守护进程 以及php fpm守护进程,两者都配置为协同工作。
所有fixture都取管理服务器根目录的fixture的名称。在 最简单的情况是使用 tmpdir_factory内置设备:
frompytest_nginximportfactories@pytest.fixture(scope="session")defnginx_server_root(tmpdir_factory):returntmpdir_factory.mktemp("nginx-server-root")nginx_proc=factories.nginx_proc("nginx_server_root")
要管理服务内容,可以创建其他模块或 功能范围的固定装置位于nginx_proc:
@pytest.fixture(scope="module")defnginx_hello_world(nginx_proc):f=open(os.path.join(nginx_proc.server_root,"index.html"),"w")f.write("Hello world! This is pytest-nginx serving on host {}, port {}.".format(nginx_proc.host,nginx_proc.port))f.close()returnnginx_proc
配置
可以通过三种方式定义设置:使用fixture factory参数, 使用命令行选项和pytest.ini配置选项。这些 设置按以下顺序处理:
- fixture factory参数
- 命令行选项
- pytest.ini配置选项
Fixture factory argument | Command line option | pytest.ini option | Default |
---|---|---|---|
host | –nginx-host | nginx_host | 127.0.0.1 |
port | –nginx-port | nginx_port | random |
nginx_exec | –nginx-exec | nginx_exec | nginx |
nginx_params | –nginx-params | nginx_params | “” |
nginx_config_template | –nginx-config-template | nginx_config_template | auto-generated |
php_fpm_exec | –php-fpm-exec | php_fpm_exec | php-fpm |
php_fpm_params | –php-fpm-params | php_fpm_params | “” |
php_fpm_config_template | –php-fpm-config-template | php_fpm_config_template | auto-generated |
显示如何指定端口号的示例:
将其作为参数传递给工厂函数:
nginx_proc=factories.nginx_proc(port=8888)
运行pytest时使用--nginx-port命令行选项:
pytest ./tests --nginx-port=8888
将nginx_port选项添加到pytest.ini文件:
[pytest]nginx_port=8888