Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在测试中创建测试对象?_Python_Unit Testing_Class Design - Fatal编程技术网

Python 如何在测试中创建测试对象?

Python 如何在测试中创建测试对象?,python,unit-testing,class-design,Python,Unit Testing,Class Design,我正在为付款流程创建单元测试。大约有20个单元测试需要编写——有些是正面的,有些是负面的 例如: payment_screen=PaymentScreen() 我有几个概念 首先-创建具有给定属性的付款人对象: payer=Payer(last_name,country_code) 国家/地区代码很重要,因为系统不允许将项目发送到其他国家/地区 第二 payer=Payer.return_correct_payer() 比如: 类别付款人: @staticmethod def retu

我正在为付款流程创建单元测试。大约有20个单元测试需要编写——有些是正面的,有些是负面的

例如:

payment_screen=PaymentScreen()
我有几个概念

首先-创建具有给定属性的付款人对象:

payer=Payer(last_name,country_code)
国家/地区代码很重要,因为系统不允许将项目发送到其他国家/地区

第二

payer=Payer.return_correct_payer()
比如:

类别付款人:

 @staticmethod
 def return_correct_payer():
 payer=Payer()
 payer.country_code='US'
 payer.last_name='Smith'
在这两种选择中

payment_screen.fill_payer_data(payer)
还有另一个概念:

在付款屏幕中,只需创建两种方法:

fill_payer_data_with_correct_data()

哪一个是最好的?或者你有另一个想法(我相信你有)

编辑

谢谢你的回复,但这不是我需要的。 我只是不想在每个测试用例中使用给定的属性创建对象Pax

我有20个测试用例,所以现在我必须写20次:

payer=Payer('Smith','US')

我不想重复我的代码

也许你想要的是某种模拟框架。然后,当您测试
PaymentScreen
时,您可以模拟
Payer
。有关python模拟框架的更多信息,请查看此处:。

嘿。
首先,您可能需要使用mock来回答这个问题,但我想展示一下如何在测试中创建对象。 因此,如果我需要一个具有给定参数的对象,我将使用BuilderPattern(实际上是从GOF修改的BuilderPattern),它如下所示:

class User  {
    private string firstName;
    private string lastName;

    public User(){};

    //now the essence
    public User withFirstName(strFirstName) {
        this.firstName = strFirstName;
        return this;
    }

    public User withLastName(strLastName) {
        this.lastName = strLastName;
        return this;
    }


    //some other stuff
}  
然后,我使用以下命令启动对象:

User testUser1 = new User()
                           .withFirstName("John")
                           .withLastName("Doe");
然后我用它做我需要的

另外,很抱歉代码不是python语法。。。但你应该得到它

User testUser1 = new User()
                           .withFirstName("John")
                           .withLastName("Doe");