使E2E测试变得容易的包。
stickybeak的Python项目详细描述
stickyback是一个端到端的测试助手库,它保存了大量的测试端点和样板代码。 通常,当出现问题时,端到端测试很难调试,因为现代微服务体系结构非常复杂。 这个库甚至可以压平最复杂的应用程序,并帮助开发人员编写易于调试和反映真实生活场景的测试。 这可以节省数百个单元和集成测试。
它是如何工作的
stickyback使用代码注入在本地python脚本环境中的远程服务器上执行任意python代码。 代码注入听起来可能很可怕,但是这个解决方案是完全安全的,因为代码注入端点只被启用 在测试或准备环境中。 执行的代码的结果(包括所有本地变量或引发的异常)将在远程服务器上进行pickle并发送回 测试脚本,其中没有问题,可用于进一步的测试和调试,就像代码在本地执行一样。 相当惊人啊:)? 目前Stickybick支持Django和烧瓶框架。
安装
pip install stickybeak
示例用法
Django应用程序(远程服务器)
fromdjango.contribimportadminfromdjango.urlsimportinclude,pathfromstickybeak.django_viewimportstickybeak_urlurlpatterns=[path('admin/',admin.site.urls),stickybeak_url,]
烧瓶应用程序(远程服务器)
fromflaskimportFlaskfromstickybeak.flask_viewimportsetupapp=Flask(__name__)setup(app)
测试应用程序(本地服务器)
fromstickybeak.injectorimportDjangoInjector,FlaskInjectordeftest_exception():injector=DjangoInjector(address='http://django-srv:8000',django_settings_module='django_srv.settings')withpytest.raises(ZeroDivisionError):injector.run_code('1/0')deftest_simple_code():injector=DjangoInjector(address='http://django-srv:8000',django_settings_module='django_srv.settings')ret:Dict[str,object]=injector.run_code('a = 123')assertret['a']==123deftest_function():injector=DjangoInjector(address='http://django-srv:8000',django_settings_module='django_srv.settings')deffun():# this code executes on the remote servera=5b=3c=a+bret:Dict[str,object]=injector.run_fun(fun)assertret['a']==5assertret['b']==3assertret['c']==8deftest_using_decorators():injector=DjangoInjector(address='http://django-srv:8000',django_settings_module='django_srv.settings')@injector.decoratordeffun():# this code executes on the remote servera=1b=4ret:Dict[str,object]=fun()assertret['a']==1assertret['b']==4deftest_django_feature():injector=DjangoInjector(address='http://django-srv:8000',django_settings_module='django_srv.settings')@injector.decoratordeffun():# this code executes on the remote serverfromapp.modelsimportCurrencyCurrency.objects.all().delete()currency=Currency()currency.name="test_currency"currency.endpoint="test_endpoint"currency.save()obj=Currency.objects.all()[0]# noqaret:Dict[str,object]=fun()obj=ret['obj']# with a little bit of python magic the object is available locally as if we were running code on the remote serverassertobj.name=="test_currency"assertobj.endpoint=="test_endpoint"# it is also available for debugger so it is possible to lookup all values and even run some class functions
开发
Stickybick使用Docker创建一个隔离的开发环境,这样您的系统就不会受到污染。
要求
为了运行本地开发,必须安装Docker和Docker组合。
启动
docker-compose up -d
登录Docker终端
./bin/terminal
代码在Docker容器和使用卷的主机之间同步,因此任何更改(pipenv installetc)都将在主机上受到影响。