Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Shalom Weberman
3,564 PointsI don't understand the error I get when running app_tests.py
I have been making improvements to the code, and tried to run the tacocat tests.
I keep getting the following error, which I don't understand.
Traceback (most recent call last):
File "app_tests.py", line 91, in test_good_login
UserModelTestCase.create_users(1)
File "app_tests.py", line 25, in create_users
password='password'
TypeError: create_user() missing 1 required positional argument: 'username'
======================================================================
ERROR: test_logged_in_menu (__main__.UserViewsTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "app_tests.py", line 118, in test_logged_in_menu
UserModelTestCase.create_users(1)
File "app_tests.py", line 25, in create_users
password='password'
TypeError: create_user() missing 1 required positional argument: 'username'
======================================================================
ERROR: test_logout (__main__.UserViewsTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "app_tests.py", line 104, in test_logout
UserModelTestCase.create_users(1)
File "app_tests.py", line 25, in create_users
password='password'
TypeError: create_user() missing 1 required positional argument: 'username'
======================================================================
FAIL: test_registration (__main__.UserViewsTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "app_tests.py", line 86, in test_registration
self.assertEqual(rv.status_code, 302)
AssertionError: 200 != 302
----------------------------------------------------------------------
Ran 11 tests in 0.176s
FAILED (failures=1, errors=8)
treehouse:~/workspace$
I do have username in my create_user():
from flask import (Flask, g, render_template, flash, redirect,
url_for)
from flask.ext.login import (LoginManager,login_user, logout_user,
login_required, current_user)
from flask.ext.bcrypt import check_password_hash
import models
import forms
DEBUG = True
PORT = 8000
HOST = '0.0.0.0'
app = Flask(__name__)
app.secret_key = 'wiedhwoijdowhkkjknijedowijdejdwejdwaejd3948d93'
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = 'login'
@login_manager.user_loader
def load_user(userid):
try:
return models.User.get(models.User.id == userid)
except models.DoesNotExist:
return None
@app.before_request
def before_request():
"""Connect to db before each request"""
g.db = models.DATABASE
g.db.connect()
g.user = current_user
@app.after_request
def after_request(response):
"""Close db connection after each request"""
g.db.close()
return response
@app.route('/register', methods=('GET', 'POST'))
def register():
form = forms.RegisterForm()
if form.validate_on_submit():
flash("Yay, you registered!", "success")
models.User.create_user(
username=form.username.data,
email=form.email.data,
password=form.password.data
)
return redirect(url_for('index'))
return render_template('register.html', form=form)
@app.route('/login', methods=('GET', 'POST'))
def login():
form = forms.LoginForm()
if form.validate_on_submit():
try:
user = models.User.get(models.User.email == form.email.data)
except models.DoesNotExist:
flash("Your email or password doesn't match!", "error")
else:
if check_password_hash(user.password, form.password.data):
login_user(user)
flash("You've been logged in!", "success")
return redirect(url_for('index'))
else:
flash("Your email or password doesn't match!", "error")
return render_template('login.html', form=form)
@app.route('/logout')
@login_required
def logout():
logout_user()
flash('You have been logged out', "success")
return redirect(url_for('index'))
@app.route('/')
def index():
tacos = models.Taco.select().limit(20)
return render_template('index.html', tacos = tacos)
@app.route('/create-taco', methods=('GET','POST'))
@login_required
def create_taco():
form = forms.CreateTacoForm()
if form.validate_on_submit():
models.Taco.create(user=g.user._get_current_object(),
extras=form.extras.data.strip(),
cheese = form.cheese.data,
protein = form.protein.data,
shell = form.shell.data
)
flash('Taco created!', "success")
return redirect(url_for('index'))
return render_template('taco.html', form=form)
if __name__ == '__main__':
models.initialize()
try:
models.User.create_user(
username='nadav',
email='nadav@reis.com',
password='password',
admin=True
)
except ValueError:
pass
app.run(debug=DEBUG, host=HOST, port=PORT)
Can you please explain what I am not understanding? Thank you in advance!
[MOD: adde additional formatting -cf]
3 Answers
Chris Freeman
Treehouse Moderator 68,468 PointsThe issue is actually the reverse: It is the test suite that is not providing the username attribute because the test suite does not expect to see username as an attribute of the User model.
The create user method being called is in the app_test.py file. Only email and password is provided, so username should not be in your User model.
class UserModelTestCase(unittest.TestCase):
@staticmethod
def create_users(count=2):
for i in range(count):
User.create_user(
email='test_{}@example.com'.format(i),
password='password'
)
Remove username from your User model, and from your User.create() calls.
Keep at it! The Tacocat Challenge is tough but fantastic once complete!
Shalom Weberman
3,564 PointsAwesome. I will take care of it. Your guidance is super helpful at a really important time for me.
Thank you so much!
Shalom Weberman
3,564 PointsAwesome. I will take care of it. Your guidance is super helpful at a really important time for me.
Thank you so much!
Todd Farr
9,547 PointsTodd Farr
9,547 PointsCan you post your models.py code?