Git Product home page Git Product logo

Comments (2)

mjallday avatar mjallday commented on August 22, 2024

We generally follow pep guidelines for python coding - http://www.python.org/dev/peps/pep-0008/#imports - in this case the format is:

  1. standard library imports
  2. related third party imports
  3. local application/library specific imports

It's a decent opinion but if a module is missing I (hope) it would never be checked in so it wouldn't break. We'll keep the current formatting since it's internally consistent across all our code bases :)

from billy.

fangpenlin avatar fangpenlin commented on August 22, 2024

Actually, when a module is missing, surely tests will be broken, it only matters where and how it breaks it. For module level imports, the whole test module is broken. For function level imports, only specific cases are broken.

For instance:

import unittest

import foobar.moda
import foobar.modb

class TestFooBar(unittest.TestCase):
    def test_case_a(self):
        foobar.moda.func1()

    def test_case_b(self):
        foobar.modb.func3()

In above code, the whole testing module will raise ImportError, no test cases will be executed. Surely the test is broken, but it breaks everything. You cannot know details about how it breaks thing. When it is in function level

import unittest

class TestFooBar(unittest.TestCase):
    def make_func1(self):
        from foobar.moda import func1
        return 

    def make_func3(self):
        from foobar.modb import func3
        return 

    def test_case_a(self):
        func1 = self.make_func1()
        func1()

    def test_case_b(self):
        func3 = self.make_func3()
        func3()

In this case, when a module is missing, still, cases will be broken, but you can know exactly which cases are broken. This brings a lots of benefit. For example, you want to upgrade the foobar module to the latest one, but the API is changed, module name is changed as well. In that case, module import will break everything. You have no idea what kind of impact that upgrading will bring. But for function level imports, you can know exactly which tests are broken by these module name changes.

And for the formatting, I also keep pep8 style in my own code base, however, I see testing code as an exception. Testing servers a different purpose than common code, surely it should follow a different guideline.

You can read the testing code of pyramid to see how they do it

https://github.com/Pylons/pyramid/tree/master/pyramid/tests

from billy.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.