Python test-driven development example and python test-driven development

Source: Internet
Author: User

Python test-driven development example and python test-driven development

The example in this article describes the python test-driven development method and shares it with you for your reference. The specific method is as follows:

import unittest from main import Sample class SampleTest(unittest.TestCase):    def setUp(self):     print "create a new Sample"     self._sample = Sample("b64e5843ca7db8199c405be565fa7f57")   def tearDown(self):     print "Destory the sample"     self._sample = None    def test_GetVirusNameFromVT(self):     "this md5 has the VT info"     aSample = Sample("b64e5843ca7db8199c405be565fa7f57")     dict_virusName = aSample._GetVirusNameFromVT()     self.assertTrue(dict_virusName!=None)   def test_GetVirusNameFromVT2(self):     "this md5 has not the VT info"     aSample = Sample("2b666ffe98e465523e514d2b93b7666a")     dict_virusName = aSample._GetVirusNameFromVT ()     self.assertTrue(len(dict_virusName) == 0)   if __name__=="__main__":   #unittest.main()   suite = unittest.TestLoader().loadTestsFromTestCase(SampleTest)   unittest.TextTestRunner(verbosity=2).run(suite) 

I hope this article will help you with Python programming.


I want to learn Python and learn a few questions.

Since you want to learn, or say you like it, you always feel that you are not trying to score points.

Python is mainly divided into python2 and python3. We recommend that you study Python 2. Download the latest 2.7 package.

There are not many tutorials. One can be used. Python is easy to get started. If you have the foundation, you can work hard. After reading the Getting Started tutorial, we will start from two aspects.
First, read the basic python library. The help document that comes with it can be used. You can check which database you like, probably all of which will be checked.
The second is to find some interesting programs to learn. There are many examples. As long as there are applications, python is often found. There are many examples of O & M testing tools, Internet, games, scientific computing, and geeks.

In the future, we will continue to practice on our own, and try to learn other people's code. Most python libraries are open-source. If it does not help, go directly to the code. In addition, test cases are often included. Test-driven development is a typical development mode of python.

Use strict programming specifications at the very beginning. Do not write programs at will. In this way, you will benefit more. Can adhere to rigorous design specifications.

Use DRY. When OO is used and when structured programming is used. Do not blindly.

Python class instantiation

1. In a python class, a private variable or function starts with _ (two underscores) but does not end with _. Private functions and variables cannot be called out of class.
Class test:
Def _ init _ (self, num ):
Self. _ num = num
PrivateTest = test (100)
PrivateTest. _ num # An error is returned.
Of course, there is also a way to adjust it, but it is not recommended to do that.
2. The variable s in the first class se () is a class variable, which can be accessed by the class itself, such as se. s, can also be accessed by various objects, and the value is unique because it exists in the class, a bit like static in C ++.
However, if an object also creates a variable named s that overwrites the class variable, the self. s is the property of the object and will not be adjusted to the class variable.
You can run it.
#-*-Coding: cp936 -*-
Class:
Name = []
Def _ init _ (self, name ):
Self. name. append (name)

Def nameMyself (self, name ):
Self. name = [name]
Print 'My name is ', self. name,' and class A1name is: ', A. name

Def test (self ):
Print "my name is", self. name
Obj = A ("num0 ")
Obj1 = A ("num1 ")
Print "obj1 'name", obj1.name # object metadata class variable name
Print "class A 'name", A. name # class variable name
Obj1.test () # The name Of The accessed class variable.
Obj1.nameMyself ('aid ') # Give yourself a name that overwrites the name of the class variable
Obj1.test () # For obj1, you can only access your own name.
Print "class a' name", A. name # The class variable still exists.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.