unittest

How does Python's unittest module detect test cases?

匿名 (未验证) 提交于 2019-12-03 03:04:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I was wondering when we run unittest.main() , how does Python know what subclasses unittest.Testcase has? For example, if I add a class FromRomanBadInput(unittest.TestCase) , how does unittest know to run this? 回答1: So I looked around in my Python27/Lib directory... unittest.main is actually an alias for a class, unittest.TestProgram . So what happens is you construct an instance of this, and its __init__ runs, which does a bunch of sanity checks and configuration, including a dynamic import of the module that you called it from (it uses the

Basic Python unittest TestSuite Question

匿名 (未验证) 提交于 2019-12-03 02:33:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Using Python 2.6, I have a very simple test in a python file in a directory: #mytest.py import unittest class BasicTests(unittest.TestCase): def test_ok(self): self.assertTrue(True) suite = unittest.TestLoader().loadTestsFromTestCase(BasicTests) I change into the directory and run "python -m unittest mytest.suite" and I get the following error: Traceback (most recent call last): File "/usr/lib/python2.6/runpy.py", line 122, in _run_module_as_main "__main__", fname, loader, pkg_name) File "/usr/lib/python2.6/runpy.py", line 34, in _run_code

Python unittests in Jenkins?

匿名 (未验证) 提交于 2019-12-03 01:54:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: How do you get Jenkins to execute python unittest cases? Is it possible to JUnit style XML output from the builtin unittest package? 回答1: sample tests: tests.py: # tests.py import random try: import unittest2 as unittest except ImportError: import unittest class SimpleTest(unittest.TestCase): @unittest.skip("demonstrating skipping") def test_skipped(self): self.fail("shouldn't happen") def test_pass(self): self.assertEqual(10, 7 + 3) def test_fail(self): self.assertEqual(11, 7 + 3) JUnit with pytest run the tests with: py.test --junitxml

Python Unittest Reporting in HTML

匿名 (未验证) 提交于 2019-12-03 01:48:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: How to generate python unittesting report in HTML format. 回答1: Again Back with Answer...... Report can generate Using HTMLTestRunner like ex: import random import unittest import HTMLTestRunner class TestSequenceFunctions(unittest.TestCase): def setUp(self): self.seq = range(10) def test_shuffle(self): # make sure the shuffled sequence does not lose any elements random.shuffle(self.seq) self.seq.sort() self.assertEqual(self.seq, range(10)) # should raise an exception for an immutable sequence self.assertRaises(TypeError, random.shuffle, (1,2

Giving parameters into TestCase from Suite in python

匿名 (未验证) 提交于 2019-12-03 01:29:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: From python documentation( http://docs.python.org/library/unittest.html ): import unittest class WidgetTestCase ( unittest . TestCase ): def setUp ( self ): self . widget = Widget ( 'The widget' ) def tearDown ( self ): self . widget . dispose () self . widget = None def test_default_size ( self ): self . assertEqual ( self . widget . size (), ( 50 , 50 ), 'incorrect default size' ) def test_resize ( self ): self . widget . resize ( 100 , 150 ) self . assertEqual ( self . widget . size (), ( 100 , 150 ), 'wrong size after resize' )

flask create_app and setUp unittest

匿名 (未验证) 提交于 2019-12-03 01:12:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: So I have setted up my flask in this way: def create_app(config_name): app = Flask(__name__, static_folder='../designs/UI', template_folder='../designs/UI', instance_relative_config=True) app.config.from_object(app_config[config_name]) app.config.from_pyfile('config.py') db.init_app(app) login_manager.init_app(app) login_manager.login_message = "You must be logged in to access this page." login_manager.login_view = "auth.login_page" migrate = Migrate(app, db) from app import models from .api import blueprint as api_blueprint app.register

Start /wait /b not exiting program when there is an error

匿名 (未验证) 提交于 2019-12-03 01:05:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have a batch file that will run several other file (lets call it procedure file) such as .bat,.exe,.py, etc... if Not Exist JobStreamUnitTest_CreateTextPython_4-27-2015.txt ( Start /wait /b C:\Users\blee2\Documents\UnitTest\CreateTextFile.py || exit %errorlevel% copy /y nul JobStreamUnitTest_CreateTextPython_4-27-2015.txt ) if Not Exist JobStreamUnitTest_CreateTextBatch_4-27-2015.txt ( Start /wait /b C:\Users\blee2\Documents\UnitTest\CreateNewFile.bat || exit %errorlevel% copy /y nul JobStreamUnitTest_CreateTextBatch_4-27-2015.txt ) if Not

argparse fails when called from unittest test

匿名 (未验证) 提交于 2019-12-03 01:03:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: In a file (say parser.py ) I have: import argparse def parse_cmdline(cmdline=None): parser = argparse.ArgumentParser() parser.add_argument('--first-param',help="Does foo.") parser.add_argument('--second-param',help="Does bar.") if cmdline is not None: args = parser.parse_args(cmdline) else: args = parser.parse_args() return vars(args) if __name__=='__main__': print parse_cmdline() Sure enough, when called from the command line it works and give me pretty much what I expect: $ ./parser.py --first-param 123 --second-param 456 {'first_param':

Java Writing unittest for exiting a program when user type quit in the console

匿名 (未验证) 提交于 2019-12-03 00:56:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I found really hard to write unit test for this method, it basically exits the program when user types a quit command. SytemExit class: public class SystemExit { public void exit(int status) { System.exit(status); } } My static method: public static void exitWhenQuitDetected() { final SystemExit systemExit = new SystemExit(); final String QUIT = "quit"; String line = ""; try { final InputStreamReader input = new InputStreamReader(System.in); final BufferedReader in = new BufferedReader(input); while (!(line.equals(QUIT))) { line = in

App Engine Unit Testing: ImportError: Start directory is not importable

匿名 (未验证) 提交于 2019-12-03 00:48:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm trying to learn unit testing with Google App Engine by using the exact code they put on the Local Unit Testing for Python page ( https://cloud.google.com/appengine/docs/python/tools/localunittesting ). I can't figure out this error, though: ImportError: Start directory is not importable: 'testmem.py' I'm just using their simple testing framework as testrunner.py and their Datastore and Memcache tests in a file called testmem.py. I call the test from the project root directory as: <me>$ python testrunner.py ~/google_appengine testmem.py