import pytest from apistellar import settings from pytest_apistellar import prop_alias from blog.blog.import_ import Import, Article article = prop_alias("blog.blog.article.article.Article") @pytest.mark.asyncio class TestImport(object): async def test_retrieve_success(self): article = ["aaaa", "[comment]: <title> (abcde)"] val = Import.retrieve("title", article) assert val == "abcde" assert len(article) == 1 assert article[0] == "aaaa" async def test_retrieve_failed(self): article = ["aaaa"] val = Import.retrieve("title", article) assert val == "" @article("load", ret_val=Article()) @article("save") async def test_insert(self, join_root_dir): article = await Import.insert( join_root_dir("test_data/一键生成API文档.md")) assert article.title == "一键生成API文档"
assert (await File.load_async()).filename == "通过工厂mock" # 这种就是有业务逻辑的替代方法,他可以通过filename不同返回不同的File对象 @pytest.mark.usefixtures("mock") @pytest.mark.prop("file.file.File.load", ret_factory="factories.mock_load", filename="还能给工厂传参") def test_load4(): assert File.load().filename == "还能给工厂传参" # 装饰器太长怎么办,取个别名来缩短导包参数 from pytest_apistellar import prop_alias file = prop_alias("file.file.File", "factories") @pytest.mark.usefixtures("mock") @file("load", ret_factory="mock_load", filename="使用别名装饰器,把前缀连起来") def test_load4(): assert File.load().filename == "使用别名装饰器,把前缀连起来" # module作用域的mock pytestmark = [ file("load", ret_factory="mock_load", filename="这是一个module作用域的mock") ] @pytest.mark.usefixtures("mock")
import pytest from toolkit.settings import SettingsLoader from pytest_apistellar import prop_alias from blog.blog.article.service import ArticleService arti_ser = prop_alias("blog.blog.article.service.ArticleService") @pytest.mark.usefixtures("mock") @pytest.mark.asyncio class TestService(object): pytestmark = [ arti_ser("settings", ret_val=SettingsLoader().load("settings")), ] @arti_ser("code", ret_val="111111") @pytest.mark.env(NEED_CODE="True") async def test_check_code_on_True(self): assert ArticleService().check_code("111111") is True @arti_ser("code", ret_val="22222") @pytest.mark.env(NEED_CODE="True") async def test_check_code_on_False(self): assert ArticleService().check_code("111111") is False @arti_ser("code", ret_val="3333") @pytest.mark.env(NEED_CODE="False") async def test_check_code_off(self): assert ArticleService().check_code("111111") is True