How to use test_setUpClass_skip method in pytest-django

Best Python code snippet using pytest-django_python

test_testsuite.py

Source:test_testsuite.py Github

copy

Full Screen

1# Copyright (c) 2009-2011 testtools developers. See LICENSE for details.2"""Test ConcurrentTestSuite and related things."""3__metaclass__ = type4import doctest5from functools import partial6import sys7import unittest8import unittest29from extras import try_import10from testtools import (11 ConcurrentTestSuite,12 ConcurrentStreamTestSuite,13 iterate_tests,14 PlaceHolder,15 TestByTestResult,16 TestCase,17 )18from testtools.compat import _b, _u19from testtools.matchers import DocTestMatches20from testtools.testsuite import FixtureSuite, iterate_tests, sorted_tests21from testtools.tests.helpers import LoggingResult22from testtools.testresult.doubles import StreamResult as LoggingStream23FunctionFixture = try_import('fixtures.FunctionFixture')24class Sample(TestCase):25 def __hash__(self):26 return id(self)27 def test_method1(self):28 pass29 def test_method2(self):30 pass31class TestConcurrentTestSuiteRun(TestCase):32 def test_broken_test(self):33 log = []34 def on_test(test, status, start_time, stop_time, tags, details):35 log.append((test.id(), status, set(details.keys())))36 class BrokenTest(object):37 # Simple break - no result parameter to run()38 def __call__(self):39 pass40 run = __call__41 original_suite = unittest.TestSuite([BrokenTest()])42 suite = ConcurrentTestSuite(original_suite, self.split_suite)43 suite.run(TestByTestResult(on_test))44 self.assertEqual([('broken-runner', 'error', set(['traceback']))], log)45 def test_trivial(self):46 log = []47 result = LoggingResult(log)48 test1 = Sample('test_method1')49 test2 = Sample('test_method2')50 original_suite = unittest.TestSuite([test1, test2])51 suite = ConcurrentTestSuite(original_suite, self.split_suite)52 suite.run(result)53 # log[0] is the timestamp for the first test starting.54 test1 = log[1][1]55 test2 = log[-1][1]56 self.assertIsInstance(test1, Sample)57 self.assertIsInstance(test2, Sample)58 self.assertNotEqual(test1.id(), test2.id())59 def test_wrap_result(self):60 # ConcurrentTestSuite has a hook for wrapping the per-thread result.61 wrap_log = []62 def wrap_result(thread_safe_result, thread_number):63 wrap_log.append(64 (thread_safe_result.result.decorated, thread_number))65 return thread_safe_result66 result_log = []67 result = LoggingResult(result_log)68 test1 = Sample('test_method1')69 test2 = Sample('test_method2')70 original_suite = unittest.TestSuite([test1, test2])71 suite = ConcurrentTestSuite(72 original_suite, self.split_suite, wrap_result=wrap_result)73 suite.run(result)74 self.assertEqual(75 [(result, 0),76 (result, 1),77 ], wrap_log)78 # Smoke test to make sure everything ran OK.79 self.assertNotEqual([], result_log)80 def split_suite(self, suite):81 return list(iterate_tests(suite))82class TestConcurrentStreamTestSuiteRun(TestCase):83 def test_trivial(self):84 result = LoggingStream()85 test1 = Sample('test_method1')86 test2 = Sample('test_method2')87 cases = lambda:[(test1, '0'), (test2, '1')]88 suite = ConcurrentStreamTestSuite(cases)89 suite.run(result)90 def freeze(set_or_none):91 if set_or_none is None:92 return set_or_none93 return frozenset(set_or_none)94 # Ignore event order: we're testing the code is all glued together,95 # which just means we can pump events through and they get route codes96 # added appropriately.97 self.assertEqual(set([98 ('status',99 'testtools.tests.test_testsuite.Sample.test_method1',100 'inprogress',101 None,102 True,103 None,104 None,105 False,106 None,107 '0',108 None,109 ),110 ('status',111 'testtools.tests.test_testsuite.Sample.test_method1',112 'success',113 frozenset(),114 True,115 None,116 None,117 False,118 None,119 '0',120 None,121 ),122 ('status',123 'testtools.tests.test_testsuite.Sample.test_method2',124 'inprogress',125 None,126 True,127 None,128 None,129 False,130 None,131 '1',132 None,133 ),134 ('status',135 'testtools.tests.test_testsuite.Sample.test_method2',136 'success',137 frozenset(),138 True,139 None,140 None,141 False,142 None,143 '1',144 None,145 ),146 ]), set(event[0:3] + (freeze(event[3]),) + event[4:10] + (None,)147 for event in result._events))148 def test_broken_runner(self):149 # If the object called breaks, the stream is informed about it150 # regardless.151 class BrokenTest(object):152 # broken - no result parameter!153 def __call__(self):154 pass155 def run(self):156 pass157 result = LoggingStream()158 cases = lambda:[(BrokenTest(), '0')]159 suite = ConcurrentStreamTestSuite(cases)160 suite.run(result)161 events = result._events162 # Check the traceback loosely.163 self.assertEqual(events[1][6].decode('utf8'),164 "Traceback (most recent call last):\n")165 self.assertThat(events[2][6].decode('utf8'), DocTestMatches("""\166 File "...testtools/testsuite.py", line ..., in _run_test167 test.run(process_result)168""", doctest.ELLIPSIS))169 self.assertThat(events[3][6].decode('utf8'), DocTestMatches("""\170TypeError: run() takes ...1 ...argument...2...given...171""", doctest.ELLIPSIS))172 events = [event[0:10] + (None,) for event in events]173 events[1] = events[1][:6] + (None,) + events[1][7:]174 events[2] = events[2][:6] + (None,) + events[2][7:]175 events[3] = events[3][:6] + (None,) + events[3][7:]176 self.assertEqual([177 ('status', "broken-runner-'0'", 'inprogress', None, True, None, None, False, None, _u('0'), None),178 ('status', "broken-runner-'0'", None, None, True, 'traceback', None,179 False,180 'text/x-traceback; charset="utf8"; language="python"',181 '0',182 None),183 ('status', "broken-runner-'0'", None, None, True, 'traceback', None,184 False,185 'text/x-traceback; charset="utf8"; language="python"',186 '0',187 None),188 ('status', "broken-runner-'0'", None, None, True, 'traceback', None,189 True,190 'text/x-traceback; charset="utf8"; language="python"',191 '0',192 None),193 ('status', "broken-runner-'0'", 'fail', set(), True, None, None, False, None, _u('0'), None)194 ], events)195 def split_suite(self, suite):196 tests = list(enumerate(iterate_tests(suite)))197 return [(test, _u(str(pos))) for pos, test in tests]198 def test_setupclass_skip(self):199 # We should support setupclass skipping using cls.skipException.200 # Because folk have used that.201 class Skips(TestCase):202 @classmethod203 def setUpClass(cls):204 raise cls.skipException('foo')205 def test_notrun(self):206 pass207 # Test discovery uses the default suite from unittest2 (unless users208 # deliberately change things, in which case they keep both pieces).209 suite = unittest2.TestSuite([Skips("test_notrun")])210 log = []211 result = LoggingResult(log)212 suite.run(result)213 self.assertEqual(['addSkip'], [item[0] for item in log])214 def test_setupclass_upcall(self):215 # Note that this is kindof-a-case-test, kindof-suite, because216 # setUpClass is linked between them.217 class Simples(TestCase):218 @classmethod219 def setUpClass(cls):220 super(Simples, cls).setUpClass()221 def test_simple(self):222 pass223 # Test discovery uses the default suite from unittest2 (unless users224 # deliberately change things, in which case they keep both pieces).225 suite = unittest2.TestSuite([Simples("test_simple")])226 log = []227 result = LoggingResult(log)228 suite.run(result)229 self.assertEqual(230 ['startTest', 'addSuccess', 'stopTest'],231 [item[0] for item in log])232class TestFixtureSuite(TestCase):233 def setUp(self):234 super(TestFixtureSuite, self).setUp()235 if FunctionFixture is None:236 self.skip("Need fixtures")237 def test_fixture_suite(self):238 log = []239 class Sample(TestCase):240 def test_one(self):241 log.append(1)242 def test_two(self):243 log.append(2)244 fixture = FunctionFixture(245 lambda: log.append('setUp'),246 lambda fixture: log.append('tearDown'))247 suite = FixtureSuite(fixture, [Sample('test_one'), Sample('test_two')])248 suite.run(LoggingResult([]))249 self.assertEqual(['setUp', 1, 2, 'tearDown'], log)250 def test_fixture_suite_sort(self):251 log = []252 class Sample(TestCase):253 def test_one(self):254 log.append(1)255 def test_two(self):256 log.append(2)257 fixture = FunctionFixture(258 lambda: log.append('setUp'),259 lambda fixture: log.append('tearDown'))260 suite = FixtureSuite(fixture, [Sample('test_one'), Sample('test_one')])261 self.assertRaises(ValueError, suite.sort_tests)262class TestSortedTests(TestCase):263 def test_sorts_custom_suites(self):264 a = PlaceHolder('a')265 b = PlaceHolder('b')266 class Subclass(unittest.TestSuite):267 def sort_tests(self):268 self._tests = sorted_tests(self, True)269 input_suite = Subclass([b, a])270 suite = sorted_tests(input_suite)271 self.assertEqual([a, b], list(iterate_tests(suite)))272 self.assertEqual([input_suite], list(iter(suite)))273 def test_custom_suite_without_sort_tests_works(self):274 a = PlaceHolder('a')275 b = PlaceHolder('b')276 class Subclass(unittest.TestSuite):pass277 input_suite = Subclass([b, a])278 suite = sorted_tests(input_suite)279 self.assertEqual([b, a], list(iterate_tests(suite)))280 self.assertEqual([input_suite], list(iter(suite)))281 def test_sorts_simple_suites(self):282 a = PlaceHolder('a')283 b = PlaceHolder('b')284 suite = sorted_tests(unittest.TestSuite([b, a]))285 self.assertEqual([a, b], list(iterate_tests(suite)))286 def test_duplicate_simple_suites(self):287 a = PlaceHolder('a')288 b = PlaceHolder('b')289 c = PlaceHolder('a')290 self.assertRaises(291 ValueError, sorted_tests, unittest.TestSuite([a, b, c]))292def test_suite():293 from unittest import TestLoader...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run pytest-django automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful