简体   繁体   English

如何在Pysys的基类中添加标准验证

[英]How to add a standard validation in the base class in Pysys

I have a number of unit tests that extends a base class. 我有一些扩展基类的单元测试。

I want to add a validate condition to all testcases. 我想为所有测试用例添加验证条件。 I know I can do in each test 我知道我可以在每次测试中做到

def validate(self):
    # The base validation 
    BaseTest.validate(self)

    # This test validation 
    self.assertGrep(file='correlator.log', exprList='TEST PASSED')

Is there a way I can do this only by modifying the base class and without having to modify all testcases? 有没有办法只能通过修改基类而不必修改所有测试用例来做到这一点?

Or is it something that needs to be done in a Runner extension? 或者是否需要在Runner扩展中完成?

TemplateMethod pattern to the rescue: TemplateMethod模式救援:

class BaseTest(unitest.Testcase):
    def validate(self):
       do_the_common_validation()
       self.validate_more()

    def validate_more(self):
        pass

Then in your subclasses you just have to override validate_more() . 然后在您的子类中,您只需覆盖validate_more()

In terms of the pysys test framework which this question is based on, there is no specific support within the framework for what you want to achieve. 就该问题所基于的pysys测试框架而言,框架内没有针对您想要实现的目标的具体支持。 The answer to this previous question (stackoverflow.com/questions/8618157/…) using metaclasses may allow you to do what you want without having to update each and every one of your tests 使用元类的上一个问题(stackoverflow.com/questions/8618157 / ...)的答案可能允许您执行您想要的操作而无需更新每个测试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM