Best Python code snippet using locust
Article.py
Source:Article.py
12__all__ = (3 'Article',4)56from . import bili78class Article(object):9 "Bç«ä¸æ ç±»,ç¨äºå表Bç«ä¸æ "10 createArticle = bili.createArticle11 deleteArticle = bili.deleteArticle12 getArticle = bili.getArticle13 articleUpcover = bili.articleUpcover14 articleCreateVote = bili.articleCreateVote15 articleCardsBvid = bili.articleCardsBvid16 articleCardsCvid = bili.articleCardsCvid17 articleMangas = bili.articleMangas1819 #æ¬ç±»åªç»§æ¿biliä¸ä¸Articleæå
³çæ¹æ³20 class Content(object):21 "ææ¬ç±»ç¨æ¥å¤çBç«å¥è©çä¸æ æäº¤æ ¼å¼"22 def __init__(self):23 self._content = ""24 def add(self, text):25 "æ·»å å
容"26 self._content = f'{self._content}{text}'27 return self28 def startH(self):29 "å¼å§ä¸ä¸ªæ é¢"30 self._content = f'{self._content}<h1>'31 return self32 def endH(self):33 "ç»æä¸ä¸ªæ é¢"34 self._content = f'{self._content}</h1>'35 return self36 def startP(self, align=""):37 "å¼å§ä¸æ®µæ£æ"38 if align == "":39 self._content = f'{self._content}<p>'40 elif align == "left":41 self._content = f'{self._content}<p style="text-align: left;">'42 elif align == "center":43 self._content = f'{self._content}<p style="text-align: center;">'44 elif align == "right":45 self._content = f'{self._content}<p style="text-align: right;">'46 else:47 self._content = f'{self._content}<p>'48 return self49 def endP(self):50 "ç»æä¸æ®µæ£æ"51 self._content = f'{self._content}</p>'52 return self53 def startD(self):54 "å¼å§ä¸æ®µå¸¦å é¤çº¿çæå"55 self._content = f'{self._content}<span style="text-decoration: line-through;">'56 return self57 def endD(self):58 "ç»æä¸æ®µå¸¦å é¤çº¿çæå"59 self._content = f'{self._content}</span>'60 return self61 def startS(self, size=16):62 "å¼å§ä¸æ®µå¤§å°ä¸ºsizeçæå"63 self._content = f'{self._content}<span class="font-size-{size}">'64 return self65 def endS(self):66 "ç»æä¸æ®µç¹å®å¤§å°çæå"67 self._content = f'{self._content}</span>'68 return self69 def startB(self):70 "å¼å§ä¸æ®µå ç²çæå"71 self._content = f'{self._content}<strong>'72 return self73 def endB(self):74 "ç»æä¸æ®µå ç²çæå"75 self._content = f'{self._content}</strong>'76 return self77 def startY(self):78 "å¼å§ä¸æ®µå¼ç¨"79 self._content = f'{self._content}<blockquote>'80 return self81 def endY(self):82 "ç»æä¸æ®µå¼ç¨"83 self._content = f'{self._content}</blockquote>'84 return self85 def br(self):86 "æå
¥æ¢è¡ï¼ä¸ç¨ç»æï¼ä¸è¬æ°æ®µé»è®¤æ¢è¡"87 self._content = f'{self._content}<p><br/></p>'88 return self89 def line(self,type=0):90 "æå
¥ä¸æ®µåå²çº¿ï¼ä¸ç¨ç»æ"91 ll = ('<figure class="img-box" contenteditable="false"><img src="//i0.hdslb.com/bfs/article/0117cbba35e51b0bce5f8c2f6a838e8a087e8ee7.png" class="cut-off-1"/></figure>',92 '<figure class="img-box" contenteditable="false"><img src="//i0.hdslb.com/bfs/article/4aa545dccf7de8d4a93c2b2b8e3265ac0a26d216.png" class="cut-off-2"/></figure>',93 '<figure class="img-box" contenteditable="false"><img src="//i0.hdslb.com/bfs/article/71bf2cd56882a2e97f8b3477c9256f8b09f361d3.png" class="cut-off-3"/></figure>',94 '<figure class="img-box" contenteditable="false"><img src="//i0.hdslb.com/bfs/article/db75225feabec8d8b64ee7d3c7165cd639554cbc.png" class="cut-off-4"/></figure>',95 '<figure class="img-box" contenteditable="false"><img src="//i0.hdslb.com/bfs/article/4adb9255ada5b97061e610b682b8636764fe50ed.png" class="cut-off-5"/></figure>',96 '<figure class="img-box" contenteditable="false"><img src="//i0.hdslb.com/bfs/article/02db465212d3c374a43c60fa2625cc1caeaab796.png" class="cut-off-6"/></figure>')97 self._content = f'{self._content}{ll[type]}'98 return self99 def startU(self):100 "å¼å§ä¸æ®µæ åºå表"101 self._content = f'{self._content}<ul class=" list-paddingleft-2">'102 return self103 def endU(self):104 "ç»æä¸æ®µæ åºå表"105 self._content = f'{self._content}</ul>'106 return self107 def startO(self):108 "å¼å§ä¸æ®µæåºå表"109 self._content = f'{self._content}<ol class=" list-paddingleft-2">'110 return self111 def endO(self):112 "ç»æä¸æ®µæåºå表"113 self._content = f'{self._content}</ol>'114 return self115 def startL(self):116 "å¼å§å表ä¸çä¸å"117 self._content = f'{self._content}<li>'118 return self119 def endL(self):120 "ç»æå表ä¸çä¸å"121 self._content = f'{self._content}</li>'122 return self123 def startA(self, url=""):124 "æå
¥ç«å
é¾æ¥,é¾æ¥è¯´ææå请ç¨addæ¹æ³æ·»å "125 self._content = f'{self._content}<a href="{url}">'126 return self127 def endA(self):128 "ç»ææå
¥ç«å
é¾æ¥"129 self._content = f'{self._content}</a>'130 return self131 def picUrl(self, url="", text="", width="", height=""):132 "æå
¥ç«å
å¾çé¾æ¥,æ·»å å¾ç说æ,æå®å¾çé¿å®½,æ¯å¦15pxï¼25%"133 self._content = f'{self._content}<figure contenteditable="false" class="img-box"><img src="{url}" '134 if width:135 self._content = f'{self._content}width="{width}" '136 if height:137 self._content = f'{self._content}height="{height}" '138 self._content = f'{self._content}/><figcaption class="caption" contenteditable="">{text}</figcaption></figure>'139 return self140 def picFile(self, article: "Articleç±»çå®ä¾", file: "æ¬å°å¾çæ件æå¾çBytes", text="", width="", height=""):141 "æå
¥æ¬å°å¾çæ件æBytes,æ·»å å¾ç说æ,æå®å¾çé¿å®½,æ¯å¦15pxï¼25%"142 ret = article.articleUpcover(file)143 picurl = ret["data"]["url"]144 picurl = picurl.replace("http", "https")145 return self.picUrl(picurl, text, width, height)146 def vote(self, article: "Articleç±»çå®ä¾", vote: "voteæ票ç»æä½åå
¸"):147 "æå
¥ç«å
æ票"148 id = article.articleCreateVote(vote)["data"]["vote_id"]149 self._content = f'{self._content}<figure class="img-box" contenteditable="false"><img src="//i0.hdslb.com/bfs/article/a9fb8e570e9683912de228446e606745cce62aa6.png" class="vote-display" data-vote-id="{id}"/><figcaption class="vote-title web-vote" contenteditable="">{vote["title"]}</figcaption></figure>'150 return self151 def card(self, Article: "Articleç±»çå®ä¾", id: "æ ¹æ®typeç±»åå¡«åid", type: "video:è§é¢æ ç¾ str,article:ä¸æ æ ç¾,fanju:çªå§æ ç¾,music:é³ä¹æ ç¾,shop:ä¼åè´æ ç¾,caricature:漫ç»æ ç¾ int,live:ç´ææ ç¾ str"):152 "æå
¥å¼ç¨æ ç¾"153 def video():154 ret = Article.articleCardsBvid(id)155 picurl = ret["data"][id]["pic"]156 picurl = picurl.replace("http", "https")157 aid = ret["data"][id]["aid"]158 return f'<figure class="img-box" contenteditable="false"><img src="{picurl}" aid="{aid}" class="video-card nomal" type="nomal"/></figure>'159 def article():160 ret = Article.articleCardsCvid(id)161 picurl = ret["data"]["banner_url"]162 picurl = picurl.replace("http", "https")163 aid = ret["data"]["id"]164 return f'<figure class="img-box" contenteditable="false"><img src="{picurl}" aid="{aid}" class="article-card" type="normal"/></figure>'165 def fanju():166 ret = Article.articleCardsCvid(id)167 picurl = ret["data"]["cover"]168 picurl = picurl.replace("http", "https")169 return f'<figure class="img-box" contenteditable="false"><img src="{picurl}" aid="{id}" class="fanju-card" type="normal"/></figure>'170 def music():171 ret = Article.articleCardsCvid(id)172 picurl = ret["data"]["cover_url"]173 picurl = picurl.replace("http", "https")174 return f'<figure class="img-box" contenteditable="false"><img src="{picurl}" aid="{id}" class="music-card" type="normal"/></figure>'175 def shop():176 ret = Article.articleCardsCvid(id)177 picurl = ret["data"]["performance_image"]178 picurl = picurl.replace("http", "https")179 return f'<figure class="img-box" contenteditable="false"><img src="{picurl}" aid="{id}" class="shop-card" type="normal"/></figure>'180 def caricature():181 ret = Article.articleMangas(id)182 picurl = ret["data"][id]["vertical_cover"]183 picurl = picurl.replace("http", "https")184 return f'<figure class="img-box" contenteditable="false"><img src="{picurl}" aid="{id}" class="caricature-card nomal" type="nomal"/></figure>'185 def live():186 ret = Article.articleCardsCvid(id)187 picurl = ret["data"]["cover"]188 picurl = picurl.replace("http", "https")189 aid = ret["data"]["room_id"]190 return f'<figure class="img-box" contenteditable="false"><img src="{picurl}" aid="{aid}" class="live-card" type="normal"/></figure>'191192 index = {193 "video": video,194 "article": article,195 "fanju": fanju,196 "music": music,197 "shop": shop,198 "caricature": caricature,199 "live": live,200 }201 if type in index:202 self._content = f'{self._content}{index[type]()}'203 return self204 def output(self):205 "è¾åº,ç¨äºArticleç±»æ交content"206 return self._content207208 def __init__(self, cookieData, tilte="", content="", aid=0, category=0, list_id=0, tid=4, original=1, image_urls="", origin_image_urls=""):209 "å建ä¸ä¸ªBç«ä¸æ è稿"210 bili.__init__(self)211 bili.login_by_cookie(self, cookieData)212 self._tilte = tilte213 self._content = content214 self._category = category215 self._list_id = list_id216 self._tid = tid217 self._original = original218 self._image_urls = image_urls219 self._origin_image_urls = origin_image_urls220 if(aid == 0):221 ret = self.createArticle(tilte, content, aid, category, list_id, tid, original, image_urls, origin_image_urls)222 self._aid = ret["data"]["aid"]223 else:224 self._aid = aid225226 def setTilte(self, tilte=0):227 "设置ä¸æ æ é¢"228 self._tilte = tilte229 def setCategory(self, category=0):230 "设置ä¸æ åç±»"231 self._category = category232 def setListId(self, list_id=0):233 "设置æéç¼å·"234 self._list_id = list_id235 def setTid(self, tid=4):236 "设置ä¸æ å°é¢ç±»å"237 self._tid = tid238 def setOriginal(self, original=1):239 "设置ä¸æ æ¯å¦ä¸ºåå,åå为1,éåå为0"240 self._original = original241 def setImage(self, origin_image_urls: str, image_urls=""):242 "设置ä¸æ 缩ç¥å¾,image_urls为缩ç¥å¾ç½åï¼origin_image_urls为缩ç¥å¾åå¾å¨æç« ä¸çç½å"243 self._origin_image_urls = origin_image_urls244 if image_urls:245 self._image_urls = image_urls246 else:247 self._image_urls = origin_image_urls248 def setContent(self, content: Content):249 "设置æç« å
容"250 self._content = content.output()251252 def getAid(self, url=False):253 "è¿åå建æç« çaidæurl,å¯éè¿urlå¨ç½é¡µä¸ä¿®æ¹æ¤æç« "254 if url:255 return f'https://member.bilibili.com/v2#/upload/text/edit?aid={self._aid}'256 else:257 return self._aid258259 def refresh(self):260 "å¦æå¨æ¬ç¨åºå¤(ä¾å¦ç½é¡µä¸)ä¿®æ¹äºæ¬æç« ,æ§è¡æ¤å½æ°åæ¥"261 ret = self.getArticle(self._aid)262 self._tilte = ret["data"]["tilte"]263 self._content = ret["data"]["content"]264 self._category = ret["data"]["category"]["id"]265 if(ret["data"]["list"] != None):266 self._list_id = ret["data"]["list"]["id"]267 self._tid = ret["data"]["template_id"]268 self._original = ret["data"]["original"]269 self._image_urls = ret["data"]["image_urls"][0]270 self._origin_image_urls = ret["data"]["origin_image_urls"][0] #è¿éå¯è½æ丢失å°é¢çé®é¢271272 def save(self):273 "ä¿åè³Bç«ä¸è稿箱,ä¸åå¸,ç½é¡µä¸å¯ç¼è¾"274 return self.createArticle(self._tilte, self._content, self._aid, self._category, self._list_id, self._tid, self._original, self._image_urls, self._origin_image_urls)275276 def submit(self):277 "åå¸è³Bç«ä¸"278 return self.createArticle(self._tilte, self._content, self._aid, self._category, self._list_id, self._tid, self._original, self._image_urls, self._origin_image_urls, True)279280 def delself(self):281 '''å é¤å½åæç« è稿'''282 self.deleteArticle(self._aid)283284 def __del__(self):
...
tests.py
Source:tests.py
1# -*- coding: utf-8 -*-2from allauth.socialaccount.tests import OAuthTestsMixin3from allauth.tests import MockedResponse, TestCase4from .provider import FlickrProvider5class FlickrTests(OAuthTestsMixin, TestCase):6 provider_id = FlickrProvider.id7 def get_mocked_response(self):8 #9 return [10 MockedResponse(11 200,12 r"""13 {"stat": "ok",14 "user": {15 "username": {16 "_content": "pennersr"},17 "id": "12345678@N00"}}18""",19 ), # noqa20 MockedResponse(21 200,22 r"""23{"person": {"username": {"_content": "pennersr"}, "photosurl": {"_content":24 "http://www.flickr.com/photos/12345678@N00/"},25 "nsid": "12345678@N00",26 "path_alias": null, "photos": {"count": {"_content": 0},27 "firstdatetaken": {"_content": null}, "views": {"_content": "28"},28 "firstdate": {"_content": null}}, "iconserver": "0",29 "description": {"_content": ""}, "mobileurl": {"_content":30 "http://m.flickr.com/photostream.gne?id=6294613"},31 "profileurl": {32 "_content": "http://www.flickr.com/people/12345678@N00/"},33 "mbox_sha1sum": {"_content":34 "5e5b359c123e54f95236209c8808d607a5cdd21e"},35 "ispro": 0, "location": {"_content": ""},36 "id": "12345678@N00",37 "realname": {"_content": "raymond penners"},38 "iconfarm": 0}, "stat": "ok"}39""",40 ),41 ] # noqa42 def test_login(self):43 account = super(FlickrTests, self).test_login()44 f_account = account.get_provider_account()45 self.assertEqual(account.user.first_name, "raymond")46 self.assertEqual(account.user.last_name, "penners")47 self.assertEqual(48 f_account.get_profile_url(),49 "http://www.flickr.com/people/12345678@N00/",50 )51 self.assertEqual(f_account.to_str(), "raymond penners")52class FlickrWithoutRealNameTests(OAuthTestsMixin, TestCase):53 """Separate test for Flickr accounts without real names"""54 provider_id = FlickrProvider.id55 def get_mocked_response(self):56 #57 return [58 MockedResponse(59 200,60 r"""61 {"stat": "ok",62 "user": {63 "username": {64 "_content": "pennersr"},65 "id": "12345678@N00"}}66""",67 ), # noqa68 MockedResponse(69 200,70 r"""71{"person": {"username": {"_content": "pennersr"}, "photosurl": {"_content":72 "http://www.flickr.com/photos/12345678@N00/"},73 "nsid": "12345678@N00",74 "path_alias": null, "photos": {"count": {"_content": 0},75 "firstdatetaken": {"_content": null}, "views": {"_content": "28"},76 "firstdate": {"_content": null}}, "iconserver": "0",77 "description": {"_content": ""}, "mobileurl": {"_content":78 "http://m.flickr.com/photostream.gne?id=6294613"},79 "profileurl": {80 "_content": "http://www.flickr.com/people/12345678@N00/"},81 "mbox_sha1sum": {"_content":82 "5e5b359c123e54f95236209c8808d607a5cdd21e"},83 "ispro": 0, "location": {"_content": ""},84 "id": "12345678@N00",85 "realname": {"_content": ""},86 "iconfarm": 0}, "stat": "ok"}87""",88 ),89 ] # noqa90 def test_login(self):91 account = super(FlickrWithoutRealNameTests, self).test_login()92 f_account = account.get_provider_account()93 self.assertEqual(account.user.first_name, "")94 self.assertEqual(account.user.last_name, "")95 self.assertEqual(96 f_account.get_profile_url(),97 "http://www.flickr.com/people/12345678@N00/",98 )...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!