python测试面试
As developers or programmers, call yourself anyone. As long as you write code there is often one thing that gets overlooked. Testing. Testing is that part of development where you’re suppose to rigorously test your applications to ensure that they meet the required functionalities.
作为开发人员或程序员,请自称任何人。 只要编写代码,通常就会忽略一件事。 测试。 测试是开发的一部分,您应该严格测试应用程序以确保它们满足所需的功能。
Let’s start of with why we even do testing in the first place:
让我们从为什么我们首先进行测试开始:
The need for less manual testing. Because the test cases are pre-written, there is no real reason to conduct manual testing anymore. Most times, especially with large applications you have to run through a lot of stages in the software to test if a certain feature works. This could be avoided with testing. Imagine having to use certain inputs every time your application runs. With testing this would be inputted automatically over each run.
需要较少的手动测试。 由于测试用例是预先编写的,因此没有真正的理由进行手动测试。 在大多数情况下,尤其是对于大型应用程序,您必须在软件中经历很多阶段才能测试某个功能是否有效。 通过测试可以避免这种情况。 想象一下,每次应用程序运行时都必须使用某些输入。 通过测试,将在每次运行中自动输入。
Allows for better structure of your code. It is of a best practice to create your test cases before development. This forces you to think of the system as a whole before you start developing.
允许更好的代码结构。 最好的做法是在开发之前创建测试用例。 这迫使您在开始开发之前就将系统视为一个整体。
Allows for faster testing in long run. When coding, the implementation of a function can change however, the output remains the same. Instead of manually testing this every time a function’s implementation changes, you can simply implement specific test cases that can run multiple test cases at a time.
从长远来看,可以进行更快的测试。 编码时,功能的实现可以更改,但是输出保持不变。 您不必简单地实现每次可以运行多个测试用例的特定测试用例,而无需每次功能实现的更改都进行手动测试。
入门 (Getting Started)
We will be focusing on a specific type of testing and that is unit testing. This specific type of test is for testing small units of code such as a function or a class.
我们将专注于特定类型的测试,即单元测试。 这种特定类型的测试用于测试较小的代码单元,例如函数或类。
To create our unit test we will be using the python standard library unittest.
This way of creating test cases uses OOP(Object Oriented Programming) by creating a class that will house all our test cases. This will allow us to run all the test cases in the class at once.
为了创建我们的单元测试,我们将使用python标准库unittest.
这种创建测试用例的方法通过创建将容纳我们所有测试用例的类来使用OOP(面向对象编程)。 这将使我们能够立即运行该类中的所有测试用例。
Note: You can use any IDE or text editor that your familiar with (default python IDE can be used). For this, I will be demonstrating using Visual Studio Code with a python extension.
注意:您可以使用您熟悉的任何IDE或文本编辑器(可以使用默认的python IDE)。 为此,我将演示如何将Visual Studio Code与python扩展一起使用。
We will first import our unit test library:
我们将首先导入我们的单元测试库:
import unittest
Now to make our class declaration:
现在进行我们的类声明:
class TestingClass(unittest.TestCase):
Note that (unittest.TestCase)
is inheritance in python. Essentially giving functionality to the class.
注意(unittest.TestCase)
是python中的继承。 本质上为类提供功能。
Inside the class add our first function:
在类内部添加我们的第一个功能:
def test_first(self): test_var = 9 + 1
self.assertEqual(11,test_var)
self.assertEqual is a method given by the previously inherited class (unittest.TestCase). This method tests if the 2 variables are equally of the same value.
self.assertEqual是先前继承的类(unittest.TestCase)给出的方法。 此方法测试两个变量的值是否相等。
Adding our test runner. This is what makes our unit test run:
添加我们的测试运行器。 这就是使我们的单元测试运行的原因:
unittest.main()
This is what the completed code should look like:
这是完整的代码,如下所示:
The above code demonstrates testing if 9 + 1 is equal to 11. If you know any basic math you should know that 9+1 = 10. Hence this test case would fail.
上面的代码演示了9 + 1是否等于11的测试。如果您了解任何基本数学运算,则应该知道9 + 1 =10。因此该测试用例将失败。
Output:
输出:
As you should have already guessed. Failure!
正如您应该已经猜到的那样。 失败!
The fix is simple. Modify the code to:
解决方法很简单。 修改代码为:
test_var = 9 + 2
Output:
输出:
测试外部功能(Testing Outside Functions)
For testing outside of functions, the previous example might not be realistic. Let’s replace the test_var
values to now come from a function. We will add a functional declaration to the top of our file.
对于功能外的测试,前面的示例可能不现实。 让我们替换一下test_var
值,使其现在来自一个函数。 我们将在文件顶部添加一个功能声明。
def add(a,b):
return a + b
This function adds 2 numbers together. Replace 9 + 2 with the function call add(9,2)
and then run your code.
此功能将两个数字相加。 用函数调用add(9,2)
替换9 + 2,然后运行您的代码。
We have only implemented one test case so far. Each function/method (both are the same thing in this case) in the TestingClass
represents a single test case.
到目前为止,我们仅实现了一个测试用例。 TestingClass
中的每个函数/方法(在这种情况下都是相同的)代表一个测试用例。
Lets add another test case called test_multiple_num_addition.
让我们添加另一个称为test_multiple_num_addition.
def test_multiple_num_addition(self):
test_var = add(10,20,30,40)
self.assertEqual(100,test_var)
Note: All the names of the test cases created should be preceded with the word “test” or else it will not be recognized by the test runner.
注意:创建的测试用例的所有名称都应以单词“ test”开头,否则测试运行程序将无法识别。
Your code should look like the following below:
您的代码应如下所示:
After running:
运行后:
This will obviously fail because the function parameters only accept 2 arguments. But what if we really wanted to add more numbers?
这显然会失败,因为函数参数仅接受2个参数。 但是,如果我们真的想增加更多的数字怎么办?
This can be solved by doing a change to the add function:
这可以通过对add函数进行更改来解决:
def add(*values):
return sum(values)
Notice the asterisk (*) left of the values
argument. This allows you to input multiple arguments as well as allowing the values to be stored as a tuple.
注意values
参数左边的星号(*)。 这样,您可以输入多个参数,也可以将值存储为元组。
Your code before execution:
执行之前的代码:
Your code while being executed:
您的代码在执行时:
And Success!
和成功!
Putting an asterisk before your variable in the function parameters is called non keyword arguments. Want more information checkout the article below.
在函数参数中的变量之前加星号称为非关键字参数。 想要更多信息,请查看下面的文章。
概要 (Summary)
Giving testing a try might give your code additional robustness. It also has the benefit of changing the way you approach development. Having a testing mindset can ensure that there are less errors in production and less repetitive manual testing during development.
尝试进行测试可能会使您的代码更加健壮。 它还具有改变开发方式的好处。 拥有测试心态可以确保在生产过程中减少错误并减少重复的手动测试。
翻译自: https://medium.com/@jordanjwilliams425/python-testing-a8156d022eef
python测试面试