-1

1. Summary

I don't understand, how I can run Python method after another actions.


2. File

Example of my .py file:

# First test
for filename in first_folder:

    if "Sasha Great" in open(filename).read():
        log.debug(filename + " Done")
    else:
        print("First test failure for " + filename)

3. Expected algoritm

3.1. Tests passed

If all tests passed, print to console:

Test for all files successful passed

3.2. Tests failed

If, for example, one of my tests failure for SashaExample.txt:

  1. print First test failure for SashaExample.txt;
  2. continue first test for another files;
    1. if there are more errors as First test failure for AnotherExample.txt, print errors to console and continue test;
  3. exit(1) after running all tests.

I need exit(1), that I can see, that I have errors in my tests, if I use Travis CI or AppVeyor.


4. Not helped

4.1. exit(1) after else

If:

for filename in first_folder:

    if "Sasha Great" in open(filename).read():
        log.debug(filename + " Done")
    else:
        print("First test failure for " + filename)
        exit(1)

Program end after first error. But I need to see all errors.

4.2. Another attempts

  1. I don't find, how I can solve my problem, in Google;
  2. I read about Python functions, methods, atexit, but I don't understand, how I can solve this problem.
Саша Черных
  • 2,561
  • 4
  • 25
  • 71
  • Are those unit tests or integration tests? For the former one, one test should be atomic and it should test one thing, e.g. "folder is good" -> (True or False). https://stackoverflow.com/a/235032/4621324 If one file fails, then "folder is bad" and there's no much sense in checking other files. If you really want to check all the files, you can "autogenerate" those methods-tests and run one after another or just make a method (not test) that will print "failing" info about all the files first and then run a test, that will fail after a first issue. – Axalix Jan 01 '18 at 17:12
  • @Axalix: thanks for the comment. My `.py` file check many `.txt` files for errors, that `.txt` files consistent with the rules of a one service. For example, it check, contains mandatory word in a file or no — as in example. // If I see all errors immediately, I can quickly fix them. I don't think, that exit program after each fail — is a good behavior in my case. – Саша Черных Jan 01 '18 at 17:23
  • @Axalix: `If you really want to check all the files, you can "autogenerate" those methods-tests and run one after another or just make a method (not test) that will print "failing" info about all the files first and then run a test, that will fail after a first issue.` — now warnings and errors only print to console for me, but tests still considered as `passed`. I can see all warnings and errors. Also I can write another `.py` file, where program will exit with exit code 1 after each fail. This file will run it after first file. But then I will have a lot of duplicated code. Thanks. – Саша Черных Jan 01 '18 at 17:35
  • @Axalix: I [**simplify example**](https://stackoverflow.com/posts/48051166/revisions). Thanks. – Саша Черных Jan 01 '18 at 18:32

2 Answers2

1

The problem appears to be that you need to call exit(1) at the end of the program if there were any failures. If that is the case, you can use a variable, say failureFound, initialized to False (because you haven't found any yet) and set to True whenever you find a failure. Then, at the end of the program, if failureFound is true, call exit(1).

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
1

This is a simple programming concept called FLAGS. Google it a bit and you will see is quite simple.

all_passed = True
# First test
for filename in first_folder:

    if "Sasha Great" in open(filename).read():
        log.debug(filename + " Done")
    else:
        print("First test failure for " + filename)
        all_passed=False
        continue

if all_passed:
    print('All test passed')
osmay88
  • 421
  • 3
  • 12
  • 1
    @Саша-Черных You can read more about it here https://www.computerhope.com/jargon/f/flag.htm – osmay88 Jan 01 '18 at 19:13