Python unittest并发执行依赖用例(修改少量源码)

在设计自动化脚本时,曾想过如何并发的执行测试用例,一般情况下是使用多线程,还行协程呢?如果使用协程,因为它异步的运行方式,我们必须先将所有主case依赖数据提前生成(为了防止有重复依赖数据,必须要生成唯一id,不然后果可会被覆盖),也就是在最后一步使用协程请求,需要改一些unittest底层源码。如果使用多线程,我们只需要将互相依赖的case按照依赖顺序组成Suite,在传入运行方法中,将底层运行的方法改成多线程,运行即可~最终效果如下,并发执行效率提高了很多:

  • 先简单讲一下协程,实现方式这次就不着重讲啦~(因为自己没有具体实现过),由于它异步的运行方式,我们必须先将所有主case依赖数据提前生成,请看下图,也就是在如图所示的位置使用协程,我在网上找到了一篇文章有相关介绍:点我   ,但此文章中只考虑到了单一接口的执行,所以在运行依赖接口时,需要在使用协程之前执行完毕~(有能力的童鞋可以自己试试呀~)

  • 接下来我们着重讲一下如何用多线程执行unittest,这次还是使用之前文章Python 40行代码实现HttpRunner(动态创建类,动态添加方法,模板解析)中的接口与基本脚本,为了让执行时间更加明显,我们把这次的接口全部加time.sleep(1),部分代码截图如下(详细请看之前文章):
    @app.route('/DepenceOne', methods=['GET'])
    def DepenceOne():
        if request.method == "GET":
            time.sleep(1)
            return jsonify({'One': A})
    
    @app.route('/DepenceTwo', methods=['POST'])
    def DepenceTwo():
        if request.method == 'POST':
            data = request.get_data()
            json_data = json.loads(data.decode("utf-8"))
            time.sleep(1)
            return jsonify({'Two': B+int(eval(json_data)['One'])})
    
    @app.route('/DepenceThr', methods=['GET'])
    def DepenceThr():
        if request.method == "GET":
            time.sleep(1)
            return jsonify({'Thr': C})
    
    @app.route('/DepenceFou', methods=['POST'])
    def DepenceFou():
        if request.method == 'POST':
            data = request.get_data()
            json_data = json.loads(data.decode("utf-8"))
            time.sleep(1)
            return jsonify({'Fou': D+int(eval(json_data)['Thr'])})
    
    @app.route('/DepenceFive', methods=['POST'])
    def DepenceFive():
        if request.method == 'POST':
            data = request.get_data()
            json_data = json.loads(data.decode("utf-8"))
            if int(eval(json_data)['Two'])+int(eval(json_data)['Fou'])==10:
                time.sleep(1)
                return jsonify({'data':'success'})
    
  • 接口依赖关系为:

  • 我们在Python 40行代码实现HttpRunner(动态创建类,动态添加方法,模板解析)中使用的是loadTestsFromModule来加载py文件中的测试类,然后全部放入一个suite中在使用HTMLTestRunner(HTMLTestReportCN.py下的一个类,由于HTMLTestReportCN被我改写过了,且里面加了很多逻辑,直接拿来用不了~原版的请百度一下下载吧~)去执行,如果采用多线程,则需要将py问中的测试类(已经组装好依赖方法)独立分成多个suite,在传入HTMLTestRunner中,并修改其中的执行方法即可,因此我们修改一下我们之前的代码,将其改为使用loadTestsFromTestCase去加载:
    for class_name in [str(i) for i  in class_dict.keys() if i not in('Log', 'ConfigReader', 'sqlconnect', 'OutputRedirector', 'Template_mixin', 'TestResult', 'HTMLTestRunner', 'TestProgram', 'main', 'Depence')]: #取出当前继承unittest的class
        n=1
        for def_data in eval(load_yaml_case()[f'{class_name}.yaml']): #取出当前继承unittest的class的testcase data
            setattr(eval(class_name), 'test_%s_func_%s' % (n,def_data['name']), getTestFunc(**def_data['request'])) #将testcase data转成def
            n+=1
        suite = unittest.TestLoader().loadTestsFromTestCase(eval(class_name)) 
  • 接下来我们看一下HTMLTestRunner中的run方法(执行suite的方法),由此可见将test=suite传入后,并传入一个result后执行。那我们现在只需要将其test改成一个list,让其中包含多个suite。
    def run(self, test):
        "Run the given test case or test suite."
        result = _TestResult(self.verbosity)
        test(result)
        self.stopTime = datetime.datetime.now()
        self.generateReport(test, result)
        # print('\nTime Elapsed: %s' % (self.stopTime - self.startTime), file=sys.stderr)
        return result
  • 修改源码,传入list,并加入多线程如下:
    def run(self, test:list):
        "Run the given test case or test suite."
        result = _TestResult(self.verbosity)
        threadings = []
        for func in test:
            t = threading.Thread(target=func, args=(result,))
            threadings.append(t)
        for j in threadings:
            j.start()
        for j in threadings:
            j.join()
        self.stopTime = datetime.datetime.now()
        self.generateReport(test, result)
        # print('\nTime Elapsed: %s' % (self.stopTime - self.startTime), file=sys.stderr)
        return result
  • 接下来我们来生成多个suite,修改Python 40行代码实现HttpRunner(动态创建类,动态添加方法,模板解析)中的源码,加入suietlist,装载不同的suite:
    suitelist=[]
    print([str(i) for i  in class_dict.keys()])
    for class_name in [str(i) for i  in class_dict.keys() if i not in('Log', 'ConfigReader', 'sqlconnect', 'OutputRedirector', 'Template_mixin', 'TestResult', 'HTMLTestRunner', 'TestProgram', 'main', 'Depence')]: #取出当前继承unittest的class
        n=1
        for def_data in eval(load_yaml_case()[f'{class_name}.yaml']): #取出当前继承unittest的class的testcase data
            setattr(eval(class_name), 'test_%s_func_%s' % (n,def_data['name']), getTestFunc(**def_data['request'])) #将testcase data转成def
            n+=1
        suite = unittest.TestLoader().loadTestsFromTestCase(eval(class_name))
        suitelist.append(suite)
  • 最后将suitelist传入经过我们修改过的HTMLTestRunner下run方法运行即可~
    with open(report_file + r'\TestReport2.html', 'wb') as f:  # 从配置文件中读取
       HTMLTestRunner(stream=f, title='Api Test', description='动态类demo', tester='ayo').run(suitelist)
    
  • 运行结果,每个接口返回时间>=1s的情况下,只花了5s,执行了25个接口。嘻嘻是不是要比单线程要快很多呢~

9,935 次浏览

“Python unittest并发执行依赖用例(修改少量源码)”的1,419个回复

  1. Hello! I know this is sort of off-topic but I needed to ask.

    Does managing a well-established blog such as yours take a large amount
    of work? I’m brand new to operating a blog but
    I do write in my diary every day. I’d like to start a blog so I will be able to share my own experience and
    thoughts online. Please let me know if you have any
    suggestions or tips for brand new aspiring bloggers.
    Thankyou!

  2. I was more than happy to uncover this web
    site. I wanted to thank you for ones time for this wonderful read!!
    I definitely appreciated every bit of it and I have you saved to fav to check out new information in your
    web site.

  3. Hi, I do believe this is an excellent website.
    I stumbledupon it 😉 I will revisit once again since I book marked it.
    Money and freedom is the best way to change, may you be rich and continue to guide other people.

  4. Hmm it looks like your site ate my first comment (it was extremely long) so I guess I’ll just sum it up what I wrote and
    say, I’m thoroughly enjoying your blog. I too am an aspiring blog
    blogger but I’m still new to the whole thing. Do you have any recommendations for newbie blog writers?
    I’d really appreciate it.

  5. Right here is the perfect web site for anyone who wishes to find out about this
    topic. You understand a whole lot its almost hard to argue with you (not that I really would want to…HaHa).
    You certainly put a new spin on a subject which
    has been discussed for many years. Excellent stuff, just great!

  6. I must express my appreciation to this writer just for bailing me out of such a setting. Right after looking out through the the net and meeting ways that were not helpful, I figured my entire life was over. Being alive without the presence of solutions to the problems you’ve resolved by means of your good website is a critical case, as well as the kind which could have in a wrong way affected my career if I hadn’t come across the blog. Your actual know-how and kindness in controlling every part was invaluable. I am not sure what I would have done if I had not come upon such a thing like this. I can also at this moment look forward to my future. Thanks for your time very much for the reliable and sensible help. I won’t be reluctant to recommend your web sites to any person who should get tips on this issue.

  7. Wow, awesome weblog structure! How lengthy have you been blogging for?
    you make running a blog glance easy. The entire look of your web site is fantastic, let
    alone the content!

  8. I wanted to put you the tiny remark to help give thanks once again with your splendid opinions you’ve featured at this time. It was really strangely open-handed with you to supply without restraint precisely what many individuals could have offered as an e-book in order to make some profit for their own end, especially considering that you could possibly have tried it if you desired. Those tips also acted like the good way to recognize that other people online have the same passion the same as my own to understand very much more with reference to this matter. I am sure there are several more pleasant moments ahead for those who read carefully your blog post.

  9. Wonderful items from you, man. I have take into accout your stuff prior to and you’re
    just too excellent. I actually like what you have acquired here, really like what you’re saying and the way in which by which you
    say it. You make it enjoyable and you still care for to keep it wise.
    I can’t wait to learn far more from you. That is actually
    a tremendous website.

  10. This design is spectacular! You definitely know how to
    keep a reader entertained. Between your wit and your videos, I was almost moved to start my own blog (well, almost…HaHa!) Fantastic job.
    I really enjoyed what you had to say, and more than that, how you presented it.

    Too cool!

  11. Needed to draft you a little remark in order to say thank you once again on the remarkable methods you’ve documented here. It is shockingly open-handed with you to allow extensively all that many of us would’ve supplied for an electronic book to help with making some profit on their own, notably given that you might have tried it in case you wanted. The secrets as well served as a great way to know that many people have similar interest just as my personal own to see lots more with respect to this condition. I think there are many more fun instances up front for individuals that read through your blog post.

  12. You are so cool! I don’t believe I’ve read through a single thing like that before.
    So nice to discover somebody with genuine thoughts on this subject.
    Really.. thank you for starting this up. This website is something that is required on the web,
    someone with a little originality!

  13. My husband and i have been quite peaceful Edward managed to finish up his inquiry by way of the ideas he received while using the web pages. It is now and again perplexing to just be releasing ideas that many men and women might have been trying to sell. And we grasp we need the blog owner to thank for this. The most important illustrations you made, the simple web site menu, the friendships your site make it easier to instill – it’s got everything astounding, and it is assisting our son and the family consider that that article is amusing, which is highly fundamental. Thank you for all the pieces!

  14. Hello there! This is kind of off topic but I need some advice from an established blog.
    Is it difficult to set up your own blog? I’m not very techincal
    but I can figure things out pretty quick. I’m thinking about setting up my own but I’m not
    sure where to begin. Do you have any tips or suggestions?
    Many thanks

  15. Its like you learn my mind! You appear to know so much approximately this, such as you
    wrote the e book in it or something. I think that you
    simply could do with a few percent to drive the message house a
    little bit, however other than that, that is
    fantastic blog. An excellent read. I will definitely be back.

  16. Excellent goods from you, man. I have understand your stuff previous to and you’re just too
    great. I actually like what you’ve acquired here, certainly like what
    you’re saying and the way in which you say it.
    You make it enjoyable and you still care for to keep
    it wise. I can’t wait to read much more from
    you. This is actually a wonderful site.

  17. I like to get my inspiration from really meaningful phrases said by truly great people like “Even while they teach, men learn”, do you know where I can find thematic compiltaions of those?

    [url]http://zzjz.51dingyou.com/index.php/2020/07/30/bahsegel-bahis/[/url]

  18. Hi are using WordPress for your blog platform?
    I’m new to the blog world but I’m trying to get started and
    create my own. Do you require any html coding expertise to make your own blog?

    Any help would be greatly appreciated!

  19. Have you ever thought about creating an ebook
    or guest authoring on other websites? I have a blog centered on the same ideas you discuss and would love to have you share some stories/information. I
    know my visitors would appreciate your work. If you are even remotely interested,
    feel free to send me an e mail.

  20. Hello! I could have sworn I’ve been to your blog before but after going through many of the articles I realized it’s new to me.

    Nonetheless, I’m certainly pleased I stumbled upon it and I’ll be bookmarking
    it and checking back regularly!

  21. I know this if off topic but I’m looking into starting my own weblog and was wondering what all is required to get set up?
    I’m assuming having a blog like yours would cost a pretty penny?

    I’m not very web smart so I’m not 100% certain. Any suggestions or advice would be
    greatly appreciated. Appreciate it

  22. What’s Happening i am new to this, I stumbled upon this
    I have found It positively helpful and it has aided me out loads.
    I hope to contribute & assist other customers like
    its aided me. Great job. cheap flights 2CSYEon

  23. I’ve been exploring for a bit for any high quality articles or weblog posts on this
    kind of space . Exploring in Yahoo I ultimately stumbled
    upon this web site. Studying this information So i’m
    glad to convey that I have a very just right uncanny feeling
    I discovered exactly what I needed. I such a lot indubitably will make sure to do not forget this site and give it a look regularly.

  24. I really love your blog.. Pleasant colors & theme.
    Did you create this amazing site yourself? Please reply back as I’m hoping to create
    my own blog and would love to learn where you got this from or
    just what the theme is named. Appreciate it!

  25. Pretty section of content. I just stumbled upon your weblog and in accession capital to assert that I acquire
    in fact enjoyed account your blog posts. Any way I’ll be subscribing to
    your augment and even I achievement you access consistently rapidly.
    cheap flights 3gqLYTc

  26. I feel this is among the so much vital information for me.
    And i’m satisfied reading your article.
    But want to statement on few general things, The web site style is wonderful,
    the articles is truly great : D. Just right task,
    cheers

  27. Pingback: viagra generic
  28. I blog often and I truly appreciate your information. Your article has really peaked my interest.
    I’m going to bookmark your blog and keep checking for new details about once per week.
    I subscribed to your Feed as well.

  29. obviously like your website however you need to take
    a look at the spelling on several of your posts. Many of them are rife with spelling problems and I to find it very troublesome to inform the reality on the other hand I’ll definitely come back again.

  30. Pingback: viagra online
  31. Pingback: viagra pill
  32. Pingback: purchase viagra
  33. Very well written article. It will be supportive to everyone who utilizes it, as well as yours truly 🙂 . Keep up the good work – can’r wait to read more posts.

  34. Pingback: generic for viagra
  35. Does your site have a contact page? I’m having trouble locating it but, I’d like to send you an email. I’ve got some suggestions for your blog you might be interested in hearing. Either way, great website and I look forward to seeing it expand over time.

  36. Hey there would you mind letting me know which web host you’re using? I’ve loaded your blog in 3 completely different web browsers and I must say this blog loads a lot faster then most. Can you recommend a good internet hosting provider at a honest price? Thank you, I appreciate it!

  37. Всем привет, хочу порекомендовать вам хороший сайт о Форексе
    С недавних пор так называемые «консультационные центры», которые аферисты из ФорексШарм расплодили по всей стране, стали активно рекламировать доселе неизвестного «инновационного брокера» ФорексШарм ( ). Посмотрим на эту контору повнимательней. Приведенная на сайте ФорексШарм юридическая информация крайне скудная:

    ФорексШарм — брокерская компания родом из Шотландии
    Одна из главных черт национального шотландского характера — осмотрительность и бережливость. Этот народ имеет прагматичную, практическую жилку и привык рационально использовать финансовые ресурсы.

    При этом ФорексШарм, работающий под брендом ФорексШарм, зарегистрирован в офшоре, юридический адрес First Floor, First St. Vincent Bank Building, James Street, Kingstown, St. Vincent and the Grenadines. Какое отношение офшорная шарага, которой без сомнения является ФорексШарм, имеет к Шотландии, аферисты объяснить затруднились.

    Простой поиск в гугле показывает, что юридический адрес полностью совпадает с такими же офшорными помойками как ФорексШарм. Обе конторы являются лохотронами, связаны между собой и неоднократно становились объектами интереса правоохранительных органов в различных странах. Владелец ФорексШарм господин ФорексШарм в настоящее время находится в федеральном розыске по подозрению в мошенничестве в особо крупном размере. ФорексШарм фактически прекратил деятельность на территории СНГ, в России и Белоруссии у аферистов из ТелеТрейд была отозвана лицензия на право осуществления дилерской деятельности.

    ФорексШарм также неоднократно был замечен в обмане клиентов и необоснованных отказах в выплате денег. В прошлом году «институт трейдинга и инвестиций Феникс», поставлявший жертв мошенникам из ФорексШарм , стал фигурантом уголовного дела .

    Информация с сайта ФорексШарм:

    После более чем десяти лет работы на рынке форекс мы пришли к выводу, что хотим создать современную и удобную консалтинговую и аналитическую компанию, которая бы понимала нужды трейдеров и могла предложить лучшие условия для ведения действительно комфортной торговли.

    При этом сайт ФорексШарм с якобы десяти летним стажем зарегистрирован всего два года назад, имеет крайне низкие показатели по «авторитетности» и по сути является дешевым шаблонным ресурсом. Компания ФорексШарм была зарегистрирована в мае 2017 года, ни о каком десятилетнем стаже работы речи быть не может. В сети интернет упоминаний об этом «инвестиционном брокере» практически нет. Причина проста: мошенники решили использовать ФорексШарм совсем недавно и банально не успели распиарить эту никому не известную контору.

    Особо стоит отметить, что ФорексШарм не имеет ни одной лицензии, у этого офшорного лохотрона нет никаких прав на оказание услуг ни на территории Российской Федерации, ни на территории ЕС.

    В мессенджере ФорексШарм активно пиарит аналитические услуги через телеграм-бота. Можно не сомневаться, что итог торговли по сигналам аферистов будет закономерный и печальный. Заработать, доверяя деньги мошенникам, невозможно.

    Вывод

    ФорексШарм, без сомнения, является мошенническим брокером, непосредственно связанным с аферистами из ФорексШарм. Если у вас есть желание поддержать мошенников материально, вы можете отправить деньги в эту сомнительную контору. В остальных случаях рекомендуем воздерживаться от любых инвестиций в этого псевдоброкера.
    Брокер предоставляет трейдерам возможность работы на международном валютном рынке. Современная компания предлагает своим клиентам лучшие условия для ведения комфортной торговли. Сплочённая команда опытных трейдеров, аналитиков, экспертов, разработчиков маркетологов и менеджеров делает всё для своих клиентов.

    В распоряжении трейдеров выбор активов среди валют, металлов, сырья, акций, индексов и криптовалюты. Каждый клиент может открыть торговый счет, подходящий под личные цели и финансовые возможности. Кредитное плечо зависит от выбранного актива от 1:1 до 1:500.

    Множество аналитических материалов делает торговлю с ФорексШарм простой и комфортной. Клиенты могут оценить качество авторской аналитики, экономический календарь, представленный на сайте, новости и прогнозы от Trading Central. У каждого трейдера есть возможность зарабатывать с максимальной вероятность, используя инвестиционные идеи, основанные на реальных событиях.

    Преимущества брокера ФорексШарм:

    1. Высокая точность аналитики.
    2. Отличные торговые условия.
    3. Возможность обучения для трейдеров с различным опытом.
    4. Отсутствие рисков и гарантия безопасности средств.
    5. Удобный вводи вывод средств.
    Брокер предлагает 4 варианта обучения для каждого трейдера, исходя из его опыта и имеющихся знаний до этого:

    1. Базовый курс: «Основы анализа финансовых рынков».
    2. Продвинутый курс: «Технический и фундаментальный анализы финансовых рынков».
    3. Профессиональный курс: «Построение торговых систем и тактик».
    4. Экспертный курс: «Мастер-класс».
    Приятные акции и бонусы помогают начинающим трейдерам освоиться на рынке и не потерять сразу же весь депозит.

    Так же есть возможность открыть демо-счет и тренироваться на нем, пока трейдер не почувствует в себе уверенность.

    Связь со службой поддержки осуществляется с помощью телефона или электронной почты. Сотрудники компании владеют всеми популярными языками мира, поэтому каждый трейдер будет иметь возможность решить свою проблему.

    http://www.forex-book.top

  38. I’m curious to find out what blog system you are using? I’m experiencing some minor security problems with my latest site and I would like to find something more risk-free. Do you have any solutions?

  39. Hi, I do believe this is a great site. I stumbledupon it 😉 I’m going to return once again since i have book marked it. Money and freedom is the best way to change, may you be rich and continue to guide other people.

  40. Heya i am for the first time here. I came across this board and I find It truly useful & it helped me out a lot. I hope to give something back and help others like you helped me.

  41. I am not sure where you’re getting your info, but good topic. I needs to spend some time learning much more or understanding more. Thanks for excellent info I was looking for this information for my mission.

  42. You’ve made some really good points there. I checked on the internet for more info about the issue and found most individuals will go along with your views on this website.

  43. Wednesday over a settlement reached with the Democratic state attorney general to allow voters to fix absentee ballots with missing information — suggesting the agreement would undermine the state’s absentee ballots. So far, more than 1 million voters in North Carolina have requested ballots to vote by mail.
    https://www.betting33.com

  44. Several states are currently mired in legislative fights over allowing elections officials to begin counting absentee ballots before Election Day. In Michigan, the Republican-controlled legislature still hasn’t allowed local officials to process absentee ballots before Election Day. The State Senate passed a bill that gives officials one extra day, but the legislation is stalled in the State House.
    https://www.ccclub700.com

  45. It’s appropriate time to make a few plans for the long run and it’s time to be happy. I have read this publish and if I could I desire to counsel you few fascinating issues or suggestions. Perhaps you can write next articles referring to this article. I wish to learn more issues about it!

  46. Pingback: buy cialis generic