title: How use pytest to unit test sqlalchemy orm classes
body: <p>I want to write some py.test code to test 2 simple sqlalchemy ORM classes that were created based on <a href="http://zetcode.com/db/sqlalchemy/orm/" rel="noreferrer">this Tutorial</a>. The problem is, how do I set a the database in py.test to a test database and rollback all changes when the tests are done? Is it possible to mock the database and run tests without actually connect to de database?</p>
  
  <p>here is the code for my classes:</p>
  
  <pre class="lang-py prettyprint-override"><code>
  from sqlalchemy import create_engine, ForeignKey
  from sqlalchemy.ext.declarative import declarative_base
  from sqlalchemy import Column, Integer, String
  from sqlalchemy.orm import sessionmaker, relationship
  
  eng = create_engine('mssql+pymssql://user:pass@host/my_database')
  
  Base = declarative_base(eng)
  Session = sessionmaker(eng)
  intern_session = Session()
  
  class Author(Base):
      __tablename__ = "Authors"
  
      AuthorId = Column(Integer, primary_key=True)
      Name = Column(String)  
      Books = relationship("Book")
  
      def add_book(self, title):
          b = Book(Title=title, AuthorId=self.AuthorId)
          intern_session.add(b)
          intern_session.commit()
  
  class Book(Base):
      __tablename__ = "Books"
  
      BookId = Column(Integer, primary_key=True)
      Title = Column(String)      
      AuthorId = Column(Integer, ForeignKey("Authors.AuthorId"))    
  
      Author = relationship("Author")                           
  
  </code></pre>