I almost agree with the idea that tests cannot be ordered. In some cases, this helps (easier to hell!) To have them in order ... after all that is the reason for the “unit” in UnitTest.
However, one alternative is to use mockout mockout objects and fix the elements that must be done before testing this particular code. You can also put a dummy function there to secure your code. For more information, check out Mock, which is now part of the standard library. Mock
Here are some YouTube videos if you haven't used Mock before.
Video 1
Video 2
Video 3
Try to use class methods to structure your code more, and then put all class methods in one main testing method.
import unittest import sqlite3 class MyOrderedTest(unittest.TestCase): @classmethod def setUpClass(cls): cls.create_db() cls.setup_draft() cls.draft_one() cls.draft_two() cls.draft_three() @classmethod def create_db(cls): cls.conn = sqlite3.connect(":memory:") @classmethod def setup_draft(cls): cls.conn.execute("CREATE TABLE players ('draftid' INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, 'first', 'last')") @classmethod def draft_one(cls): player = ("Hakeem", "Olajuwon") cls.conn.execute("INSERT INTO players (first, last) VALUES (?, ?)", player) @classmethod def draft_two(cls): player = ("Sam", "Bowie") cls.conn.execute("INSERT INTO players (first, last) VALUES (?, ?)", player) @classmethod def draft_three(cls): player = ("Michael", "Jordan") cls.conn.execute("INSERT INTO players (first, last) VALUES (?, ?)", player) def test_unordered_one(self): cur = self.conn.execute("SELECT * from players") draft = [(1, u'Hakeem', u'Olajuwon'), (2, u'Sam', u'Bowie'), (3, u'Michael', u'Jordan')] query = cur.fetchall() print query self.assertListEqual(query, draft) def test_unordered_two(self): cur = self.conn.execute("SELECT first, last FROM players WHERE draftid=3") result = cur.fetchone() third = " ".join(result) print third self.assertEqual(third, "Michael Jordan")
Jason Wirth Aug 16 2018-12-12T00: 00Z
source share