Code Monkey home page Code Monkey logo

ipython-nose-vfa-2018-v2's Introduction

import sys, os
import logging
import unittest

log = logging.getLogger()

from nose import core, loader

#logging.basicConfig(level=logging.DEBUG)

from types import ModuleType
def adder(a, b):
    return a + b

def test_nothing():
    assert 1+2 == 2

def test_adder():
    assert adder(3, 4) == 7
    assert adder(-3, -5) == -8
    
import random
def test_random_generated_tests():
    for _ in range(random.randint(1, 100)):
        yield lambda: None

flake = False
def test_flake():
    global flake
    flake = not flake
    assert flake
class MyProgram(core.TestProgram):
    # XXX yuck: copy superclass runTests() so we can instantiate our own runner class;
    # can't do it early because we don't have access to nose's config object.
    def runTests(self):
        self.testRunner = MyRunner(self.config)
        # the rest is mostly duplicate code ;-(
        plug_runner = self.config.plugins.prepareTestRunner(self.testRunner)
        if plug_runner is not None:
            self.testRunner = plug_runner
        self.result = self.testRunner.run(self.test)
        self.success = self.result.wasSuccessful()
        return self.success

class MyResult(unittest.TestResult):
    def make_bar(self, tests, failing):
        return '''<div>
                <div style="background:red; width:%dpx">&nbsp;</div>
                <div style="background:green; width:%dpx">&nbsp;</div>
                </div>''' % (
            failing * 10, (tests - failing) * 10)

    def make_table_of_tests(self, tests):
        table = '<table>'
        for test in tests:
            table += '<tr><td>%s</td><td><pre><![CDATA[%s]]></pre></td>' % (
                str(test[0]), test[1])
        table += '</table>'
        return table
    
    def _repr_html_(self):
        if self.errors or self.failures:
            not_successes = len(self.errors) + len(self.failures)
            return self.make_bar(self.testsRun, not_successes) + \
                '''
                <h2 style="color:red">Errors</h2>%s
                <h2 style="color:red">Failures</h2>%s''' % (
                    self.make_table_of_tests(self.errors),
                    self.make_table_of_tests(self.failures))
        else:
            return self.make_bar(self.testsRun, 0) + '<div>%d/%d&nbsp;tests&nbsp;passed</div>' % (
                self.testsRun, self.testsRun)

class MyRunner(object):
    def __init__(self, config):
        self.config = config

    def run(self, test):
        result = MyResult()
        if hasattr(result, 'startTestRun'):   # python 2.7
            result.startTestRun()
        test(result)
        if hasattr(result, 'stopTestRun'):
            result.stopTestRun()
        self.config.plugins.finalize(result)
        self.result = result
        return result
test_module = ModuleType('test_module')
test_module.__dict__.update(get_ipython().user_ns)

ldr = loader.TestLoader()
tests = ldr.loadTestsFromModule(test_module)
#print("discovered: %r" % list(tests._tests))

tprog = MyProgram(argv=['dummy'], exit=False, suite=tests)
tprog.result
 
 

Errors

Failures

__main__.test_nothing
", line 5, in test_nothing
    assert 1+2 == 2
AssertionError
]]>

ipython-nose-vfa-2018-v2's People

Contributors

eloraburns avatar gward avatar embray avatar jeffkatzy avatar juliantaylor avatar fdeheeger avatar

Watchers

 avatar Rishikesh Tirumala avatar James Cloos avatar  avatar Victoria Thevenot avatar  avatar Joe Cardarelli avatar Katie Burke avatar Sara Tibbetts avatar The Learn Team avatar Sophie DeBenedetto avatar  avatar Antoin avatar Alex Griffith avatar  avatar Amanda D'Avria avatar  avatar Nicole Kroese  avatar Lisa Jiang avatar  avatar

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.