Question

This is a question about how assertTrue() works and why this seemingly stray typo doesn't cause problems when I run it as a test. I'm learning Flask and how to unit test, so please bear with me if I get some terminology wrong.

Why does the following test pass regardless of presence/absence of 'b' before 'Please login'?

Using code that the tutorial gives me:

    def test_login_page_loads(self): 
        tester = app.test_client(self) 
        response = tester.get('/login', content_type='html/text') 
        self.assertTrue(b'Please login' in response.data)

I thought the 'b' in self.assertTrue(b'Please login' in response.data) was a typo, but it passes the test with or without the character there.

For reference, this is (most of) what it's testing:

<h1>Please login</h1>
  <br>
  <form action="" method="post">
    <input type="text" placeholder="Username" name="username" value="{{
      request.form.username }}">
     <input type="password" placeholder="Password" name="password" value="{{
      request.form.password }}">
    <input class="btn btn-default" type="submit" value="Login">
  </form>

Looking at documents for unittest I didn't see anything special, simple Googling yields nothing valuable, the posts here are related to logical flow issues, and I'm not sure where to go after that.

From what I can see there are a few possible answers: 1)'b' is an option for the function assertTrue(), 2) that I don't understand, it's being skipped by the function, 3) or the test is 'evaluating as true' regardless of the input.

Was it helpful?

Solution

b in this case means the string is a bytestring. See the answer to this question for more details:

What does the 'b' character do in front of a string literal?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top